You are here

function _name_field_settings_form in Name Field 7

Implements hook_field_settings_form().

1 call to _name_field_settings_form()
name_field_settings_form in ./name.module
Implements hook_field_settings_form().

File

includes/name.content.inc, line 18
Provides additional Field functionality for the name module.

Code

function _name_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  $form = array(
    '#tree' => TRUE,
  );
  $components = _name_translations();
  $form['components'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Components'),
    '#default_value' => $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['minimum_components'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Minimum components'),
    '#default_value' => $settings['minimum_components'],
    '#required' => TRUE,
    '#element_validate' => array(
      '_name_field_minimal_component_requirements',
    ),
    '#description' => t('The minimal set of components required before the field is considered completed enough to save.'),
    '#options' => $components,
  );
  $form['labels'] = array();
  $form['max_length'] = array();
  $autocomplete_sources = module_invoke_all('name_data_sources');
  $autocomplete_sources_options = array();
  foreach ($autocomplete_sources as $ac_source => $ac_settings) {
    if (!empty($ac_settings['autocomplete callback'])) {
      $autocomplete_sources_options[$ac_source] = $ac_settings['name'];
    }
  }
  $has_data = field_has_data($field);
  foreach ($components as $key => $title) {
    $min_length = 1;
    if ($has_data) {
      $min_length = $settings['max_length'][$key];
      if ($field['storage']['type'] == 'field_sql_storage') {
        try {
          $table = 'field_data_' . $field['field_name'];
          $column = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table][$key];
          $min_length = db_query("SELECT MAX(CHAR_LENGTH({$column})) AS len FROM {$table}")
            ->fetchField();
          if ($min_length < 1) {
            $min_length = 1;
          }
        } catch (Exception $e) {
        }
      }
    }
    $form['max_length'][$key] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum length for !title', array(
        '!title' => $title,
      )),
      '#default_value' => $settings['max_length'][$key],
      '#required' => TRUE,
      '#size' => 10,
      '#min_size' => $min_length,
      '#description' => t('The maximum length of the field in characters. This must be between !min and 255.', array(
        '!min' => $min_length,
      )),
      '#element_validate' => array(
        '_name_validate_varchar_range',
      ),
    );
    $form['labels'][$key] = array(
      '#type' => 'textfield',
      '#title' => t('Label for !title', array(
        '!title' => $title,
      )),
      '#default_value' => $settings['labels'][$key],
      '#required' => TRUE,
    );
    $form['autocomplete_source'][$key] = array(
      '#type' => 'checkboxes',
      '#title' => t('Autocomplete options'),
      '#default_value' => $settings['autocomplete_source'][$key],
      '#description' => t("This defines what autocomplete sources are available to the field."),
      '#options' => $autocomplete_sources_options,
    );
    foreach ($autocomplete_sources as $ac_source => $ac_settings) {
      if (!empty($ac_settings['components']) && !in_array($key, $ac_settings['components'])) {
        unset($form['autocomplete_source'][$key]['#options'][$ac_source]);
      }
    }
    $has_ac_options = count($form['autocomplete_source'][$key]['#options']);
    $form['autocomplete_separator'][$key] = array(
      '#type' => 'textfield',
      '#title' => t('Autocomplete separator for !title', array(
        '!title' => $title,
      )),
      '#default_value' => $has_ac_options ? $settings['autocomplete_separator'][$key] : '',
      '#size' => 10,
    );
    if (!$has_ac_options) {
      $form['autocomplete_separator'][$key]['#disabled'] = TRUE;
    }
  }
  $form['allow_family_or_given'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow a single valid given or family value to fulfill the minimum component requirements for both given and family components.'),
    '#default_value' => !empty($settings['allow_family_or_given']),
  );

  // @todo Grouping & grouping sort.
  // @todo Allow reverse free tagging back into the vocabulary.
  $title_options = implode("\n", array_filter(explode("\n", $settings['title_options'])));
  $form['title_options'] = array(
    '#type' => 'textarea',
    '#title' => t('!title options', array(
      '!title' => $components['title'],
    )),
    '#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'.", array(
      '!title' => $components['title'],
    )),
  );
  $generational_options = implode("\n", array_filter(explode("\n", $settings['generational_options'])));
  $form['generational_options'] = array(
    '#type' => 'textarea',
    '#title' => t('!generational options', array(
      '!generational' => $components['generational'],
    )),
    '#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: '----'.", array(
      '!generational' => $components['generational'],
    )),
  );
  if (module_exists('taxonomy')) {

    // @todo Make the labels more generic.
    // 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['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 machine-name or 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['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 machine-name or 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($settings['sort_options']) ? $settings['sort_options'] : array(
    'title' => 'title',
    'generational' => '',
  );
  $form['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' => '',
    )),
  );
  $form['#element_validate'] = array(
    '_name_field_settings_form_validate',
  );
  return $form;
}