You are here

function computed_field_field_settings_form in Computed Field 7

Implements hook_field_settings_form().

File

./computed_field.module, line 111
Functionality for the computed field.

Code

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('<p>The variables available to your code include: <code>@fields</code>. To set the value of the field, set <code>@entity_field</code>.  For multi-value computed fields continue with <code>@entity_field_multi</code>.  Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (<code>@field_a</code> and <code>@field_b</code>) in a node entity:</p> !example <p>Alternately, this code can be supplied by your own custom function named: <code>@compute_func(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)</code>.</p>', 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' => '<p><code>$field_a = field_get_items($entity_type, $entity, "field_a");<br />
        $field_b = field_get_items($entity_type, $entity, "field_b");<br />
        $entity_field[0]["value"] = $field_a[0]["value"] + $field_b[0]["value"];</code></p>
      ',
      '@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('<strong>This field is COMPUTED using <code>@compute_func()</code>.</strong>', 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 <code>@display_output</code> variable, which will be printed when the field is displayed. The raw computed value of the field is in <code>@value</code>.  <strong>Note:</strong> this code has no effect if you use the "Raw computed value" display formatter.<p> Alternately, this code can be supplied by your own custom function named: <code>@display_func($field, $entity_field_item, $entity_lang, $langcode, $entity)</code>.  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('<strong>This field is DISPLAYED using <code>@display_func()</code>.</strong>', 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('<strong>**This field currently has stored data, so modifications to its DB settings are not allowed.**</strong>'),
    );
  }
  $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('<strong>Only</strong> valid for <strong>varchar</strong> or <strong>text</strong> 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('<strong>Only</strong> valid for <strong>int</strong> or <strong>float</strong> 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('<strong>Only</strong> valid for <strong>decimal</strong> 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('<strong>Only</strong> valid for <strong>decimal</strong> 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;
}