You are here

function computed_field_field_settings in Computed Field 5

Same name and namespace in other branches
  1. 6 computed_field.module \computed_field_field_settings()

Implementation of cck hook_field_settings()

File

./computed_field.module, line 22

Code

function computed_field_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();

      // these next 3 have been moved from widget to field, so they copy default values from widget
      $form['code'] = array(
        '#type' => 'textarea',
        '#rows' => 15,
        '#title' => t('Computed Code'),
        '#description' => t('The variables available to your code are: ') . '<code>&amp;$node, $field, and &amp;$node_field</code>' . t('. To set the value of the field, set ') . '<code>$node_field[0][\'value\']</code>' . t('. 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: ') . '<code>$node_field[0][\'value\'] = $node->field_a[0][\'value\'] + $node->field_b[0][\'value\'];</code>',
        '#default_value' => !empty($field['code']) ? $field['code'] : '$node_field[0][\'value\'] = "";',
      );
      $form['display'] = array(
        '#type' => 'checkbox',
        '#title' => t('Display this field'),
        '#default_value' => is_numeric($field['code']) ? $field['code'] : TRUE,
      );
      $form['display_format'] = array(
        '#type' => 'textarea',
        '#title' => t('Display Format'),
        '#description' => t('This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item[\'value\'].  Note: this code has no effect if you use the "Computed Value" formatter option.'),
        '#default_value' => !empty($field['display_format']) ? $field['display_format'] : '$display = $node_field_item[\'value\'];',
      );
      $form['store'] = array(
        '#type' => 'checkbox',
        '#title' => t('Store using the database settings below'),
        '#default_value' => is_numeric($field['store']) ? $field['store'] : 1,
      );
      $form['database'] = array(
        '#type' => 'fieldset',
        '#title' => t('Database Storage Settings'),
      );
      $form['database']['data_type'] = array(
        '#type' => 'radios',
        '#title' => t('Data Type'),
        '#description' => t('The SQL datatype to store this field in.'),
        '#default_value' => !empty($field['data_type']) ? $field['data_type'] : 'varchar',
        '#options' => array(
          'int' => 'int',
          'float' => 'float',
          'varchar' => 'varchar',
          'text' => 'text',
          'longtext' => 'longtext',
        ),
        '#required' => FALSE,
      );
      $form['database']['data_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Data Length'),
        '#default_value' => !empty($field['data_length']) ? $field['data_length'] : NULL,
        '#required' => FALSE,
      );
      $form['database']['data_default'] = array(
        '#type' => 'textfield',
        '#title' => t('Default Value'),
        '#default_value' => $field['data_default'],
        '#required' => FALSE,
      );
      $form['database']['data_not_NULL'] = array(
        '#type' => 'checkbox',
        '#title' => t('Not NULL'),
        '#default_value' => is_numeric($field['data_not_NULL']) ? $field['data_not_NULL'] : FALSE,
      );
      $form['database']['data_sortable'] = array(
        '#type' => 'checkbox',
        '#title' => t('Sortable'),
        '#default_value' => is_numeric($field['data_sortable']) ? $field['data_sortable'] : TRUE,
      );
      return $form;
    case 'validate':
      if ($field['store']) {
        if (empty($field['data_type'])) {
          form_set_error('data_type', t('To store this field in the database, please specify a data type.'));
        }
        if (!($field['data_type'] == 'text' || $field['data_type'] == 'longtext') && empty($field['data_length'])) {
          form_set_error('data_length', t('To store this field in the database, please specify the data length.'));
        }
      }
      break;
    case 'save':
      return array(
        'code',
        'display',
        'display_format',
        'store',
        'data_type',
        'data_length',
        'data_not_NULL',
        'data_default',
        'data_sortable',
      );
    case 'database columns':
      if ($field['store']) {
        $columns = array(
          'value' => array(),
        );
        if ($field['data_type'] == 'longtext') {
          $columns['value']['type'] = 'text';
          $columns['value']['size'] = 'big';
        }
        else {
          $columns['value']['type'] = isset($field['data_type']) ? $field['data_type'] : 'varchar';
        }

        // text and longtext should not have a length, so we ignore it
        if (!($field['data_type'] == 'text' || $field['data_type'] == 'longtext')) {
          $columns['value']['length'] = isset($field['data_length']) ? $field['data_length'] : 32;
        }
        $columns['value']['not NULL'] = isset($field['data_not_NULL']) ? $field['data_not_NULL'] : TRUE;
        $columns['value']['sortable'] = isset($field['data_sortable']) ? $field['data_sortable'] : FALSE;
        if ($field['data_default'] != '') {
          $columns['value']['default'] = $field['data_default'];
        }
      }
      return $columns;
    case 'filters':
      return array(
        'default' => array(
          'name' => t('Default'),
          'operator' => 'views_handler_operator_gtlt',
        ),
      );
    case 'callbacks':
      return array(
        'view' => CONTENT_CALLBACK_CUSTOM,
      );
    case 'views data':
      $allowed_values = content_allowed_values($field);
      if (count($allowed_values)) {
        $data = content_views_field_views_data($field);
        $db_info = content_database_info($field);
        $table_alias = content_views_tablename($field);

        // Swap the filter handler to the 'in' operator.
        $data[$table_alias][$field['field_name'] . '_value']['filter']['handler'] = 'content_handler_filter_many_to_one';
        return $data;
      }
  }
}