Permalink
Browse files

initial work on hidden_field

  • Loading branch information...
0 parents commit 518bbf64679c6ff3096fc917524828ec3f4aa617 @skwashd committed Jan 2, 2011
Showing with 68 additions and 0 deletions.
  1. +4 −0 README.txt
  2. +3 −0 hidden_field.info
  3. +61 −0 hidden_field.module
@@ -0,0 +1,4 @@
+This module allows Fields to have hidden and value widget types.
+
+The initial code was provided by chx and has since been polished
+and refined.
@@ -0,0 +1,3 @@
+name = Hidden Field Widgets
+description = Adds support hidden field widgets.
+core = 7.x
@@ -0,0 +1,61 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Defines simple hidden field widgets.
+ */
+
+/**
+ * Implements hook_field_widget_info().
+ */
+function hidden_field_field_widget_info() {
+ return array(
+ 'hidden_hidden' => array(
+ 'label' => t('Hidden field'),
+ ),
+ 'hidden_value' => array(
+ 'label' => t('Server side only'),
+ ),
+ );
+}
+
+/**
+ * Implements hook_field_widget_form().
+ */
+function hidden_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
+
+ $base = $element;
+ if ($instance['widget']['type'] == 'hidden_hidden') {
+ $type = 'hidden';
+ $key = '#default_value';
+ }
+ else {
+ $type = 'value';
+ $key = '#value';
+ }
+ foreach ($field['columns'] as $column => $field_column) {
+ // Try to make sure the #value is not NULL so the user can't tamper a #type
+ // value.
+ $value = '';
+ if (isset($items[$delta]['value'])) {
+ $value = $items[$delta]['value'];
+ }
+ elseif (isset($items[$delta][$field['field_name']])) {
+ $value = $items[$delta][$field['field_name']];
+ }
+ elseif (isset($field_column['default'])) {
+ $value = $field_column['default'];
+ }
+ elseif (in_array($field_column['type'], array('int', 'numeric', 'float'))) {
+ $value = 0;
+ }
+
+ $element[$column] = $base + array(
+ '#type' => $type,
+ '#value' => $value,
+ );
+ }
+
+ return $element;
+}

0 comments on commit 518bbf6

Please sign in to comment.