Select.php in Select (or other) 8
File
src/Element/Select.php
View source
<?php
namespace Drupal\select_or_other\Element;
use Drupal\Core\Form\FormStateInterface;
class Select extends ElementBase {
public static function processSelectOrOther(&$element, FormStateInterface $form_state, &$complete_form) {
$element = parent::processSelectOrOther($element, $form_state, $complete_form);
static::setSelectType($element);
static::addEmptyOption($element);
static::addStatesHandling($element);
return $element;
}
protected static function setSelectType(array &$element) {
$element['select']['#type'] = 'select';
}
protected static function addEmptyOption(array &$element) {
if (!isset($element['#no_empty_option']) || !$element['#no_empty_option']) {
if (!$element['#required'] || empty($element['#default_value'])) {
$element['select']['#empty_value'] = '';
}
}
}
protected static function addStatesHandling(array &$element) {
if (!$element['#multiple']) {
$element['other']['#states'] = static::prepareState('visible', $element['#name'] . '[select]', 'value', 'select_or_other');
}
else {
$element['select']['#multiple'] = TRUE;
$element['select']['#attached'] = [
'library' => [
'select_or_other/multiple_select_states_hack',
],
];
}
}
}
Classes
Name |
Description |
Select |
Provides a form element with a select box and other option. |