form_options_attributes.module in Form Options Attributes 8
Same filename and directory in other branches
Form options attributes module.
File
form_options_attributes.moduleView source
<?php
/**
* @file
* Form options attributes module.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\Core\Render\Element;
/**
* Implements hook_theme().
*/
function form_options_attributes_theme() {
return [
'form_options_attributes__select__options_attributes' => [
'render element' => 'elements',
'base hook' => 'select',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function form_options_attributes_theme_suggestions_select_alter(array &$suggestions, array $variables) {
if (!empty($variables['element']['#options_attributes'])) {
array_push($suggestions, 'form_options_attributes__select__options_attributes');
}
}
/**
* Implements hook_element_info_alter().
*/
function form_options_attributes_element_info_alter(array &$info) {
$info['checkboxes']['#process'][] = 'form_options_attributes_form_process_option_attributes';
$info['radios']['#process'][] = 'form_options_attributes_form_process_option_attributes';
}
/**
* Prepares variables for select element templates.
*/
function form_options_attributes_preprocess_form_options_attributes__select__options_attributes(&$variables) {
$element = $variables['element'];
Element::setAttributes($element, [
'id',
'name',
'size',
]);
RenderElement::setAttributes($element, [
'form-select',
]);
$variables['attributes'] = $element['#attributes'];
$variables['options'] = form_select_options($element);
foreach ($element['#options'] as $key => $option) {
if (is_array($option)) {
// This option in an opt group.
foreach ($option as $sub_key => $sub_option) {
if (!empty($element['#options_attributes'][$key][$sub_key])) {
$attributes = $element['#options_attributes'][$key][$sub_key];
$variables['options_attributes'][$key][$sub_key] = new Attribute($attributes);
}
}
}
elseif (!empty($element['#options_attributes'][$key])) {
// This is a normal option with attributes set.
$attributes = $element['#options_attributes'][$key];
$variables['options_attributes'][$key] = new Attribute($attributes);
}
}
}
/**
* Processes a checkboxes or radios form element to add options_attributes
* @param array $element
* @return array
*/
function form_options_attributes_form_process_option_attributes($element) {
foreach (Element::children($element) as $key) {
if (!empty($element['#options_attributes'][$key]) && is_array($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
Name | 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_preprocess_form_options_attributes__select__options_attributes | Prepares variables for select element templates. |
form_options_attributes_theme | Implements hook_theme(). |
form_options_attributes_theme_suggestions_select_alter | Implements hook_theme_suggestions_HOOK_alter(). |