array( 'label' => t('Computed'), 'description' => t('Create field data via PHP code.'), 'settings' => array( 'code' => '$entity_field[0][\'value\'] = "";', 'display_format' => '$display_output = $entity_field_item[\'value\'];', 'store' => 1, 'recalculate' => FALSE, 'database' => array( 'data_type' => 'varchar', 'data_length' => 32, 'data_size' => 'normal', 'data_precision' => 10, 'data_scale' => 2, 'data_not_NULL' => FALSE, 'data_default' => NULL, 'data_index' => FALSE, ), ), 'default_widget' => 'computed', 'default_formatter' => 'computed_field_plain', // If we followed the core convention of separate fields for each data // type we could make Entity API happy by just setting a property_type. // Instead we have to use our own callback to determine the type then // rerun theirs to setup the rest of the field properties. 'property_callbacks' => array('computed_field_entity_property_callback'), ), ); } /** * Callback to setup Entity API's field properties. */ function computed_field_entity_property_callback(&$info, $entity_type, $field, $instance, $field_type) { $property_types = array( 'int' => 'integer', 'float' => 'decimal', 'numeric' => 'decimal', 'varchar' => 'text', 'text' => 'text', 'longtext' => 'text', ); if (isset($field['columns']['value']) && isset($property_types[$field['columns']['value']['type']])) { // Entity API's defaults are pretty good so set the property_type and let them do the work for us. $field_type['property_type'] = $property_types[$field['columns']['value']['type']]; entity_metadata_field_default_property_callback($info, $entity_type, $field, $instance, $field_type); // The only thing is that a setter doesn't make sense, so let's disable it. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']]; unset($property['setter callback']); } } /** * Implements hook_field_settings_form(). */ function computed_field_field_settings_form($field, $instance, $has_data) { $form = array(); $compute_func = 'computed_field_' . $field['field_name'] . '_compute'; $display_func = 'computed_field_' . $field['field_name'] . '_display'; $settings = $field['settings']; $form['#element_validate'] = array('computed_field_field_settings_form_validate'); $form['code'] = array( '#type' => 'textarea', '#rows' => 15, '#title' => t('Computed Code (PHP)'), '#description' => t('

The variables available to your code include: @fields. To set the value of the field, set @entity_field. For multi-value computed fields continue with @entity_field_multi. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (@field_a and @field_b) in a node entity:

!example

Alternately, this code can be supplied by your own custom function named: @compute_func(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items).

', array( '@fields' => '&$entity_field, $entity_type, $entity, $field, $instance, $langcode, and $items', '@entity_field' => '$entity_field[0][\'value\']', '@entity_field_multi' => '$entity_field[1][\'value\']', '@field_a' => 'field_a', '@field_b' => 'field_b', '!example' => '

$field_a = field_get_items($entity_type, $entity, "field_a");
$field_b = field_get_items($entity_type, $entity, "field_b");
$entity_field[0]["value"] = $field_a[0]["value"] + $field_b[0]["value"];

', '@compute_func' => $compute_func, )), '#default_value' => !empty($settings['code']) ? $settings['code'] : '$entity_field[0][\'value\'] = "";', '#access' => !function_exists($compute_func), ); if (function_exists($compute_func)) { $form['compute_func'] = array( '#type' => 'item', '#markup' => t('This field is COMPUTED using @compute_func().', array('@compute_func' => $compute_func)), ); } $form['display_format'] = array( '#type' => 'textarea', '#title' => t('Display Code (PHP)'), '#description' => t('This code should assign a string to the @display_output variable, which will be printed when the field is displayed. The raw computed value of the field is in @value. Note: this code has no effect if you use the "Raw computed value" display formatter.

Alternately, this code can be supplied by your own custom function named: @display_func($field, $entity_field_item, $entity_lang, $langcode, $entity). Return the value to be displayed. Original value is in $entity_field_item[\'value\'].', array( '@display_output' => '$display_output', '@value' => '$entity_field_item[\'value\']', '@display_func' => $display_func, )), '#default_value' => !empty($settings['display_format']) ? $settings['display_format'] : '$display_output = $entity_field_item[\'value\'];', '#access' => !function_exists($display_func), ); if (function_exists($display_func)) { $form['display_func'] = array( '#type' => 'item', '#markup' => t('This field is DISPLAYED using @display_func().', array('@display_func' => $display_func)), ); } $form['recalculate'] = array( '#type' => 'checkbox', '#title' => t('Recalculate the field value every time.'), '#description' => t('By default, Drupal will cache the value of this field even if it is not stored in the database (and even if Page Caching is disabled). This option will cause computed_field to recalculate the value every time this field is displayed. For example, a time-based calculated value may change more often than field cache is cleared. (Note that Drupal page caching will still cache the field value.)'), '#default_value' => is_numeric($settings['recalculate']) ? $settings['recalculate'] : FALSE, ); $form['store'] = array( '#type' => 'checkbox', '#title' => t('Store value in the database'), '#description' => t('The value will be stored in the database with the settings below. As a result, it will only be recalculated when the entity is updated. This option is required when accessing the field through Views.'), '#default_value' => is_numeric($settings['store']) ? $settings['store'] : 1 , '#disabled' => $has_data, ); $form['database'] = array('#type' => 'fieldset', '#title' => t('Database Storage Settings')); if ($has_data) { $form['database']['warning'] = array( '#type' => 'item', '#markup' => t('**This field currently has stored data, so modifications to its DB settings are not allowed.**'), ); } $form['database']['data_type'] = array( '#type' => 'radios', '#title' => t('Data Type'), '#description' => t('The SQL datatype to store this field in.'), '#default_value' => !empty($settings['database']['data_type']) ? $settings['database']['data_type'] : 'varchar', '#options' => array( 'varchar' => 'varchar', 'text' => 'text', 'longtext' => 'longtext', 'int' => 'int', 'float' => 'float', 'numeric' => 'decimal', ), '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_length'] = array( '#type' => 'textfield', '#title' => t('Data Length (varchar/text)'), '#description' => t('Only valid for varchar or text fields. The length of the field stored in the database.'), '#default_value' => !empty($settings['database']['data_length']) ? $settings['database']['data_length'] : 32, '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_size'] = array( '#type' => 'select', '#title' => t('Data Size (int/float)'), '#description' => t('Only valid for int or float fields. The size of the field stored in the database.'), '#default_value' => !empty($settings['database']['data_size']) ? $settings['database']['data_size'] : 'normal', '#options' => array( 'tiny' => 'tiny', 'small' => 'small', 'medium' => 'medium', 'normal' => 'normal', 'big' => 'big', ), '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_precision'] = array( '#type' => 'select', '#title' => t('Decimal Precision (decimal)'), '#description' => t('Only valid for decimal fields. The total number of digits to store in the database, including those to the right of the decimal.'), '#options' => drupal_map_assoc(range(10, 32)), '#default_value' => !empty($settings['database']['data_precision']) ? $settings['database']['data_precision'] : 10, '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_scale'] = array( '#type' => 'select', '#title' => t('Decimal Scale (decimal)'), '#description' => t('Only valid for decimal fields. The number of digits to the right of the decimal. '), '#options' => drupal_map_assoc(range(0, 10)), '#default_value' => !empty($settings['database']['data_scale']) ? $settings['database']['data_scale'] : 2, '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_default'] = array( '#type' => 'textfield', '#title' => t('Default Value'), '#default_value' => $settings['database']['data_default'], '#required' => FALSE, '#disabled' => $has_data, ); $form['database']['data_not_NULL'] = array( '#type' => 'checkbox', '#title' => t('Not NULL'), '#default_value' => is_numeric($settings['database']['data_not_NULL']) ? $settings['database']['data_not_NULL'] : FALSE, '#disabled' => $has_data, ); $form['database']['data_index'] = array( '#type' => 'checkbox', '#title' => t('Index computed values in the database (Does not apply to text or longtext fields.)'), '#default_value' => is_numeric($settings['database']['data_index']) ? $settings['database']['data_index'] : FALSE, '#disabled' => $has_data, ); return $form; } /** * #element_validate callback for computed_field_field_settings_form(). */ function computed_field_field_settings_form_validate($element, &$form_state) { $settings = $form_state['values']['field']['settings']; if ($settings['store']) { if (empty($settings['database']['data_type'])) { form_set_error('field][settings][data_type', t('To store this field in the database, please specify a data type.')); } if (($settings['database']['data_type'] == 'text' || $settings['database']['data_type'] == 'varchar') && empty($settings['database']['data_length'])) { form_set_error('field][settings][database][data_length', t('To store this field in the database, please specify the data length.')); } if (($settings['database']['data_type'] == 'int' || $settings['database']['data_type'] == 'float') && (!empty($settings['database']['data_default']) && !is_numeric($settings['database']['data_default']))) { form_set_error('field][settings][database][data_default', t('Your default value should be numeric given your data type.')); } } } /** * Implements hook_field_load(). */ function computed_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { $settings = $field['settings']; // Compute field values on load if they aren't stored in the database. if (!$settings['store']) { foreach ($entities as $etid => $entity) { _computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]); } } } /** * Implements hook_field_prepare_view(). */ function computed_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { $settings = $field['settings']; // Compute field values in case user is "previewing" an entity. foreach ($entities as $etid => $entity) { if ((isset($entity->op) && $entity->op == 'Preview') || $settings['recalculate']) { _computed_field_compute_value($entity_type, $entity, $field, $instances, $langcode, $items[$etid]); } } } /** * Implements hook_field_insert(). */ function computed_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items); } /** * Implements hook_field_update(). */ function computed_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, $items); } /** * Implements hook_field_widget_info(). */ function computed_field_field_widget_info() { return array( 'computed' => array( 'label' => t('Computed'), 'field types' => array('computed'), 'behaviors' => array( 'multiple values' => FIELD_BEHAVIOR_CUSTOM, 'default value' => FIELD_BEHAVIOR_NONE, ), ), ); } /** * Implements hook_field_widget_form(). */ function computed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { // If there are no items yet, add a null item value to avoid // preview errors when selecting a different language. if (empty($items)) { $items[0]['value'] = NULL; } foreach ($items as $item_delta => $item) { $element[$item_delta]['value'] = array( '#type' => 'value', '#tree' => TRUE, '#default_value' => isset($item['value']) ? $item['value'] : NULL, ); } return $element; } /** * Implements hook_field_formatter_info(). */ function computed_field_field_formatter_info() { return array( 'computed_field_unsanitized' => array( 'label' => t('Unsanitized'), 'field types' => array('computed'), ), 'computed_field_plain' => array( 'label' => t('Plain text'), 'field types' => array('computed'), ), 'computed_field_markup' => array( 'label' => t('Filtered markup'), 'field types' => array('computed'), ), 'computed_field_computed_value' => array( 'label' => t('Raw value, no display code'), 'field types' => array('computed'), ), ); } /** * Implements hook_field_formatter_view(). */ function computed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); $data_to_display = FALSE; // Special case formatter that returns the raw computed values without any display code processing. if ($display['type'] == "computed_field_computed_value") { foreach ($items as $delta => $item) { if (!isset($entity_field_item['value'])) { $entity_field_item['value'] = NULL; } $element[$delta] = array('#markup' => $item['value']); } return $element; } // Other display formatters which run through display code processing. // Check if the value is to be formatted by a display function outside the DB. $display_func = 'computed_field_' . $field['field_name'] . '_display'; $display_in_code = function_exists($display_func) ? TRUE : FALSE; // Loop the items to display. foreach ($items as $delta => $item) { // For "some" backwards compatibility. $entity_field_item = $item; // Setup a variable with the entity language if available. if (isset($entity->language)) { $entity_lang = $entity->language; } else { $entity_lang = LANGUAGE_NONE; } // If there are value "holes" in the field array let's set the value to NULL. // to avoid undefined index errors in typical PHP display code. if (!isset($entity_field_item['value'])) { $entity_field_item['value'] = NULL; } // Execute the display code. $display_output = NULL; if ($display_in_code) { $display_output = $display_func($field, $entity_field_item, $entity_lang, $langcode, $entity); } else { eval($field['settings']['display_format']); } // Track if any of our items produce non-empty output. if (!empty($display_output) || is_numeric($display_output)) { $data_to_display = TRUE; } // Output the formatted display item. switch ($display['type']) { case 'computed_field_unsanitized': $element[$delta] = array('#markup' => $display_output); break; case 'computed_field_plain': $element[$delta] = array('#markup' => check_plain($display_output)); break; case 'computed_field_markup': $element[$delta] = array('#markup' => check_markup($display_output)); break; } } // If all items are empty then we should not return anything. This helps // ensure that empty fields are not displayed at all. This check does not // apply to fields stored in the DB as those are instead checked on save. if (isset($field['settings']['store']) && !$field['settings']['store'] && !$data_to_display) { return; } return $element; } /** * Implements hook_field_is_empty(). */ function computed_field_field_is_empty($item, $field) { unset($empty); // This will depend on the class of data type. switch ($field['settings']['database']['data_type']) { case 'int': case 'float': case 'numeric': // For numbers, the field is empty if the value isn't numeric. $empty = !is_numeric($item['value']); break; case 'varchar': case 'text': case 'longtext': // For strings, the field is empty if it doesn't match the empty string. $empty = ($item['value'] === ""); break; } return $empty; } /** * Private function to compute the fields value. */ function _computed_field_compute_value($entity_type, $entity, $field, $instance, $langcode, &$items) { $settings = $field['settings']; // Setup a variable with the field values. $entity_field =& $items; // Setup a variable with the entity language if available. if (isset($entity->language)) { $entity_lang = $entity->language; } else { $entity_lang = LANGUAGE_NONE; } // Allow the value to be computed from code not stored in DB. $compute_func = 'computed_field_' . $field['field_name'] . '_compute'; if (function_exists($compute_func)) { $compute_func($entity_field, $entity_type, $entity, $field, $instance, $langcode, $items); } else { if (isset($settings['code'])) { eval($settings['code']); } } }