You are here

function text_field_settings in Content Construction Kit (CCK) 6

Same name in this branch
  1. 6 examples/simple_field.php \text_field_settings()
  2. 6 examples/example_field.php \text_field_settings()
  3. 6 modules/text/text.module \text_field_settings()
Same name and namespace in other branches
  1. 5 text.module \text_field_settings()
  2. 6.3 modules/text/text.module \text_field_settings()
  3. 6.2 modules/text/text.module \text_field_settings()

Implementation of hook_field_settings().

Handle the settings for a field.

Parameters

$op: The operation to be performed. Possible values:

  • "form": Display the field settings form.
  • "validate": Check the field settings form for errors.
  • "save": Declare which fields to save back to the database.
  • "database columns": Declare the columns that content.module should create and manage on behalf of the field. If the field module wishes to handle its own database storage, this should be omitted.
  • "filters": Declare the Views filters available for the field. (this is used in CCK's default Views tables definition) They always apply to the first column listed in the "database columns" array.

$field: The field on which the operation is to be performed.

Return value

This varies depending on the operation.

  • "form": an array of form elements to add to the settings page.
  • "validate": no return value. Use form_set_error().
  • "save": an array of names of form elements to be saved in the database.
  • "database columns": an array keyed by column name, with arrays of column information as values. This column information must include "type", the MySQL data type of the column, and may also include a "sortable" parameter to indicate to views.module that the column contains ordered information. TODO : Details of other information that can be passed to the database layer can be found in the API for the Schema API.
  • "filters": an array of 'filters' definitions as expected by views.module (see Views Documentation). When providing several filters, it is recommended to use the 'name' attribute in order to let the user distinguish between them. If no 'name' is specified for a filter, the key of the filter will be used instead.

File

examples/example_field.php, line 153
These hooks are defined by field modules, modules that define a new kind of field for insertion in a content type.

Code

function text_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $options = array(
        0 => t('Plain text'),
        1 => t('Filtered text (user selects input format)'),
      );
      $form['text_processing'] = array(
        '#type' => 'radios',
        '#title' => t('Text processing'),
        '#default_value' => is_numeric($field['text_processing']) ? $field['text_processing'] : 0,
        '#options' => $options,
      );
      $form['max_length'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum length'),
        '#default_value' => is_numeric($field['max_length']) ? $field['max_length'] : '',
        '#required' => FALSE,
        '#description' => t('The maximum length of the field in characters. Leave blank for an unlimited size.'),
      );
      $form['allowed_values'] = array(
        '#type' => 'textarea',
        '#title' => t('Allowed values list'),
        '#default_value' => !empty($field['allowed_values']) ? $field['allowed_values'] : '',
        '#required' => FALSE,
        '#rows' => 10,
        '#description' => t('The possible values this field can contain. Enter one value per line, in the format key|label. The key is the value that will be stored in the database and it must match the field storage type, %type. The label is optional and the key will be used as the label if no label is specified.', array(
          '%type' => $field['type'],
        )),
      );
      $form['advanced_options'] = array(
        '#type' => 'fieldset',
        '#title' => t('PHP code'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['advanced_options']['allowed_values_php'] = array(
        '#type' => 'textarea',
        '#title' => t('Code'),
        '#default_value' => !empty($field['allowed_values_php']) ? $field['allowed_values_php'] : '',
        '#rows' => 6,
        '#description' => t('Advanced usage only: PHP code that returns a keyed array of allowed values. Should not include <?php ?> delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.'),
      );
      return $form;
    case 'save':
      return array(
        'text_processing',
        'max_length',
        'allowed_values',
        'allowed_values_php',
      );
    case 'database columns':
      if (empty($field['max_length']) || $field['max_length'] > 255) {
        $columns['value'] = array(
          'type' => 'text',
          'size' => 'big',
          'not null' => FALSE,
          'sortable' => TRUE,
        );
      }
      else {
        $columns['value'] = array(
          'type' => 'varchar',
          'length' => $field['max_length'],
          'not null' => FALSE,
          'sortable' => TRUE,
        );
      }
      if (!empty($field['text_processing'])) {
        $columns['format'] = array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        );
      }
      return $columns;
    case 'filters':
      $allowed_values = content_allowed_values($field);
      if (count($allowed_values)) {
        return array(
          'default' => array(
            'list' => $allowed_values,
            'list-type' => 'list',
            'operator' => 'views_handler_operator_or',
            'value-type' => 'array',
          ),
        );
      }
      else {
        return array(
          'like' => array(
            'operator' => 'views_handler_operator_like',
            'handler' => 'views_handler_filter_like',
          ),
        );
      }
      break;
  }
}