You are here

form_options_attributes.module in Form Options Attributes 7

Same filename and directory in other branches
  1. 8 form_options_attributes.module
  2. 2.0.x form_options_attributes.module

Form options attributes module.

File

form_options_attributes.module
View source
<?php

/**
 * @file
 * Form options attributes module.
 */

/**
 * Implements hook_theme_registry_alter().
 */
function form_options_attributes_theme_registry_alter(&$theme_registry) {
  $theme_registry['select']['function'] = 'form_options_attributes_select';
}

/**
 * Implements hook_element_info_alter().
 */
function form_options_attributes_element_info_alter(&$elements) {
  $elements['checkboxes']['#process'][] = 'form_options_attributes_form_process_option_attributes';
  $elements['radios']['#process'][] = 'form_options_attributes_form_process_option_attributes';
}

/**
 * Returns HTML for a select form element.
 * Copied from theme_select(), and added patch from:
 * https://www.drupal.org/node/414562
 *
 * It is possible to group options together; to do this, change the format of
 * $options to an associative array in which the keys are group labels, and the
 * values are associative arrays in the normal $options format.
 *
 * @param $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #title, #value, #options, #description, #extra,
 *     #multiple, #required, #name, #attributes, #size.
 *
 * @ingroup themeable
 */
function form_options_attributes_select($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array(
    'id',
    'name',
    'size',
  ));
  _form_set_class($element, array(
    'form-select',
  ));
  return '<select' . drupal_attributes($element['#attributes']) . '>' . form_options_attributes_form_select_options($element) . '</select>';
}

/**
 * Converts a select form element's options array into HTML.
 * Copied from form_select_options(), and added patch from:
 * https://www.drupal.org/node/414562
 *
 * @param $element
 *   An associative array containing the properties of the element.
 * @param $choices
 *   Mixed: Either an associative array of items to list as choices, or an
 *   object with an 'option' member that is an associative array. This
 *   parameter is only used internally and should not be passed.
 *
 * @return
 *   An HTML string of options for the select form element.
 */
function form_options_attributes_form_select_options($element, $choices = NULL) {
  if (!isset($choices)) {
    $choices = $element['#options'];
  }

  // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
  // isset() fails in this situation.
  $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
  $value_is_array = $value_valid && is_array($element['#value']);
  $options = '';
  foreach ($choices as $key => $choice) {
    if (is_array($choice)) {
      $options .= '<optgroup label="' . check_plain($key) . '">';
      $options .= form_options_attributes_form_select_options($element, $choice);
      $options .= '</optgroup>';
    }
    elseif (is_object($choice)) {
      $options .= form_options_attributes_form_select_options($element, $choice->option);
    }
    else {
      $key = (string) $key;
      if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || $value_is_array && in_array($key, $element['#value']))) {
        $selected = ' selected="selected"';
      }
      else {
        $selected = '';
      }
      if (!empty($element['#options_attributes'][$key])) {
        $options_attributes = ' ' . drupal_attributes($element['#options_attributes'][$key]);
      }
      else {
        $options_attributes = '';
      }
      $options .= '<option value="' . check_plain($key) . '"' . $selected . $options_attributes . '>' . check_plain($choice) . '</option>';
    }
  }
  return $options;
}

/**
 * Processes a checkboxes or radios form element to add options_attributes.
 */
function form_options_attributes_form_process_option_attributes($element) {
  foreach (element_children($element) as $key) {
    if (!empty($element['#options_attributes'][$key])) {
      if (!empty($element[$key]['#attributes'][$key]) && is_array($element[$key]['#attributes'][$key])) {
        $element[$key]['#attributes'] = array_merge($element[$key]['#attributes'], $element['#options_attributes'][$key]);
      }
      else {
        $element[$key]['#attributes'] = $element['#options_attributes'][$key];
      }
    }
  }
  return $element;
}

Functions

Namesort descending Description
form_options_attributes_element_info_alter Implements hook_element_info_alter().
form_options_attributes_form_process_option_attributes Processes a checkboxes or radios form element to add options_attributes.
form_options_attributes_form_select_options Converts a select form element's options array into HTML. Copied from form_select_options(), and added patch from: https://www.drupal.org/node/414562
form_options_attributes_select Returns HTML for a select form element. Copied from theme_select(), and added patch from: https://www.drupal.org/node/414562
form_options_attributes_theme_registry_alter Implements hook_theme_registry_alter().