1.20.x options.element.inc _form_options_expand($element)

Logic function for form_options_expand(). Do not call directly.

See also

form_options_expand()

File

modules/field/modules/options/options.element.inc, line 13
All logic for options form elements.

Code

function _form_options_expand($element) {
  $element['#options'] = isset($element['#options']) ? $element['#options'] : array();
  $element['#multiple'] = isset($element['#multiple']) ? $element['#multiple'] : FALSE;

  $element['#tree'] = TRUE;
  $element['#theme'] = 'options';

  $path = backdrop_get_path('module', 'options');
  $element['#attached']['library'] = array(
    array('options', 'options'),
  );

  // Add the key type toggle checkbox.
  if (!isset($element['custom_keys']) && $element['#key_type'] != 'custom' && !empty($element['#key_type_toggle'])) {
    $element['custom_keys'] = array(
      '#title' => is_string($element['#key_type_toggle']) ? $element['#key_type_toggle'] : t('Customize keys'),
      '#type' => 'checkbox',
      '#default_value' => $element['#key_type_toggled'],
      '#attributes' => array('class' => array('key-type-toggle')),
      '#description' => t('Customizing the keys will allow you to save one value internally while showing a different option to the user.'),
    );
  }

  // Add the multiple value toggle checkbox.
  if (!isset($element['multiple']) && !empty($element['#multiple_toggle'])) {
    $element['multiple'] = array(
      '#title' => is_string($element['#multiple_toggle']) ? $element['#multiple_toggle'] : t('Allow multiple values'),
      '#type' => 'checkbox',
      '#default_value' => !empty($element['#multiple']),
      '#attributes' => array('class' => array('multiple-toggle')),
      '#description' => t('Multiple values will let users select multiple items in this list.'),
    );
  }
  // If the element had a custom interface for toggling whether or not multiple
  // values are accepted, make sure that form_type_options_value() knows to use
  // it.
  if (isset($element['multiple']) && empty($element['#multiple_toggle'])) {
    $element['#multiple_toggle'] = TRUE;
  }

  // Add the main textarea for adding options.
  if (!isset($element['options'])) {
    $element['options_field'] = array(
      '#type' => 'textarea',
      '#resizable' => TRUE,
      '#cols' => 60,
      '#rows' => 5,
      '#required' => isset($element['#required']) ? $element['#required'] : FALSE,
      '#description' => t('List options one option per line.'),
      '#attributes' => $element['#options_readonly'] ? array('readonly' => 'readonly') : array(),
      '#wysiwyg' => FALSE, // Prevent CKeditor from trying to hijack.
    );

    // If validation fails, reload the user's text even if it's not valid.
    if (isset($element['#value']['options_text'])) {
      $element['options_field']['#value'] = $element['#value']['options_text'];
    }
    // Most of the time, we'll be converting the options array into the text.
    else {
      $element['options_field']['#value'] = isset($element['#options']) ? form_options_to_text($element['#options'], $element['#key_type']) : '';
    }


    if ($element['#key_type'] == 'mixed' || $element['#key_type'] == 'numeric' || $element['#key_type'] == 'custom') {
      $element['options_field']['#description'] .= ' ' . t('Key-label pairs may be specified by separating each option with pipes, such as <em>key|label</em>.');
    }
    elseif ($element['#key_type_toggle']) {
      $element['options_field']['#description'] .= ' ' . t('If the %toggle field is checked, key-label pairs may be specified by separating each option with pipes, such as <em>key|label</em>.', array('%toggle' => $element['custom_keys']['#title']));
    }
    if ($element['#key_type'] == 'numeric') {
      $element['options_field']['#description'] .= ' ' . t('This field requires all specified keys to be integers.');
      $element['options_field']['#description'] .= ' ' . t('The label is optional: if a line contains a single number, it will be used as key and label.');
    }
    $element['options_field']['#description'] .= ' ' . t('Customizing the keys will allow you to save one value internally while showing a different option to the user.');
  }

  // Add the field for storing default values.
  if ($element['#default_value_allowed'] && !isset($element['default_value_field'])) {
    $element['default_value_field'] = array(
      '#title' => t('Default value'),
      '#type' => 'textfield',
      '#size' => 60,
      '#maxlength' => 1024,
      '#value' => isset($element['#default_value']) ? ($element['#multiple'] ? implode(', ', (array) $element['#default_value']) : $element['#default_value']) : '',
      '#description' => t('Specify the keys that should be selected by default.'),
    );
    if ($element['#multiple']) {
      $element['default_value_field']['#description'] .= ' ' . t('Multiple default values may be specified by separating keys with commas.');
    }
  }

  // Add the field for storing a default value pattern.
  if ($element['#default_value_pattern']) {
    $element['default_value_pattern'] = array(
      '#type' => 'hidden',
      '#value' => $element['#default_value_pattern'],
      '#attributes' => array('class' => array('default-value-pattern')),
    );
  }

  // Pass allowed HTML tags to JS.
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array('optionsElement' => array('allowedtags' => '<div class="description">' . t('Allowed HTML tags in labels: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())) . '</div>')),
  );

  // Remove properties that will confuse the FAPI.
  unset($element['#options']);

  return $element;
}