You are here

function name_field_settings in Name Field 6

Implementation of hook_field_settings().

File

./name.module, line 625
Defines an API for displaying and inputing names.

Code

function name_field_settings($op, $field) {
  _name_defaults($field, 'field_settings');
  switch ($op) {
    case 'form':
      $form = array(
        'field_settings' => array(
          '#tree' => TRUE,
        ),
      );
      $components = _name_translations();
      $form['field_settings']['components'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Components'),
        '#default_value' => $field['field_settings']['components'],
        '#required' => TRUE,
        '#description' => t('Only selected components will be activated on this field. All non-selected components / component settings will be ignored.'),
        '#options' => $components,
        '#element_validate' => array(
          '_name_field_minimal_component_requirements',
        ),
      );
      $form['field_settings']['minimum_components'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Minimum components'),
        '#default_value' => $field['field_settings']['minimum_components'],
        '#required' => TRUE,
        '#element_validate' => array(
          '_name_field_minimal_component_requirements',
        ),
        '#description' => t('The minimal set of components required before considered the name field to be incomplete.'),
        '#options' => $components,
      );
      $form['field_settings']['labels'] = array();
      $form['field_settings']['max_length'] = array();
      foreach ($components as $key => $title) {
        $form['field_settings']['max_length'][$key] = array(
          '#type' => 'textfield',
          '#title' => t('Maximum length for !title', array(
            '!title' => $title,
          )),
          '#default_value' => $field['field_settings']['max_length'][$key],
          '#required' => FALSE,
          '#size' => 10,
          '#description' => t('The maximum length of the field in characters. This must be between 1 and 255.'),
          '#element_validate' => array(
            '_name_validate_integer_positive',
          ),
        );
        $form['field_settings']['labels'][$key] = array(
          '#type' => 'textfield',
          '#title' => t('Label for !title', array(
            '!title' => $title,
          )),
          '#default_value' => $field['field_settings']['labels'][$key],
          '#required' => TRUE,
        );
      }

      // TODO - Grouping & grouping sort
      // TODO - Allow reverse free tagging back into the vocabulary.
      $title_options = implode("\n", array_filter(explode("\n", $field['field_settings']['title_options'])));
      $form['field_settings']['title_options'] = array(
        '#type' => 'textarea',
        '#title' => t('Title options'),
        '#default_value' => $title_options,
        '#required' => TRUE,
        '#description' => t("Enter one title per line. Prefix a line using '--' to specify a blank value text. For example: '--Please select a title'."),
      );
      $generational_options = implode("\n", array_filter(explode("\n", $field['field_settings']['generational_options'])));
      $form['field_settings']['generational_options'] = array(
        '#type' => 'textarea',
        '#title' => t('Generational options'),
        '#default_value' => $generational_options,
        '#required' => TRUE,
        '#description' => t("Enter one generational suffix option per line. Prefix a line using '--' to specify a blank value text. For example: '----'."),
      );
      if (module_exists('taxonomy')) {

        // Generational suffixes may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary id. Terms that exceed the maximum length of the generational suffix are not added to the options list.
        $form['field_settings']['title_options']['#description'] .= ' ' . t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary id. Terms that exceed the maximum length of the %label are not added to the options list.", array(
          '%label_plural' => t('Titles'),
          '%label' => t('Title'),
        ));
        $form['field_settings']['generational_options']['#description'] .= ' ' . t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary id. Terms that exceed the maximum length of the %label are not added to the options list.", array(
          '%label_plural' => t('Generational suffixes'),
          '%label' => t('Generational suffix'),
        ));
      }
      $sort_options = is_array($field['field_settings']['sort_options']) ? $field['field_settings']['sort_options'] : array(
        'title' => 'title',
        'generational' => '',
      );
      $form['field_settings']['sort_options'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Select field sort options'),
        '#default_value' => $sort_options,
        '#description' => t("This enables sorting on the options after the vocabulary terms are added and duplicate values are removed."),
        '#options' => _name_translations(array(
          'title' => '',
          'generational' => '',
        )),
      );
      return $form;
    case 'validate':

      // Validates options against the title / generational sizes.
      _element_validate_options_size($field['field_settings']['title_options'], $field['field_settings']['max_length']['title'], t('Title options'));
      _element_validate_options_size($field['field_settings']['generational_options'], $field['field_settings']['max_length']['generational'], t('Generational options'));
      _name_field_settings_validate($field);
      break;
    case 'save':
      return array(
        'field_settings',
      );
    case 'database columns':
      $components = array_filter($field['field_settings']['components']);
      $columns = array();
      foreach (_name_translations($components) as $key => $title) {
        $max = $field['field_settings']['max_length'][$key];
        $columns[$key] = array(
          'type' => 'varchar',
          'length' => is_numeric($max) ? $max : 255,
          'not null' => FALSE,
          'sortable' => TRUE,
          'views' => TRUE,
        );
      }
      return $columns;
    case 'views data':
      $data = content_views_field_views_data($field);
      $db_info = content_database_info($field);
      $table_alias = content_views_tablename($field);

      // Make changes to $data as needed here.
      return $data;
  }
}