function EntityReference::valueFormAddSelect

Adds a select element to the form.

Parameters

array $form: Associative array containing the structure of the form, passed by reference.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

1 call to EntityReference::valueFormAddSelect()
EntityReference::valueForm in core/modules/views/src/Plugin/views/filter/EntityReference.php
Options form subform for setting options.

File

core/modules/views/src/Plugin/views/filter/EntityReference.php, line 491

Class

EntityReference
Filters a view by entity references.

Namespace

Drupal\views\Plugin\views\filter

Code

protected function valueFormAddSelect(array &$form, FormStateInterface $form_state) : void {
    $is_exposed = $form_state->get('exposed');
    $options = $this->getValueOptions();
    $default_value = (array) $this->value;
    if ($is_exposed) {
        $identifier = $this->options['expose']['identifier'];
        if (!empty($this->options['expose']['reduce'])) {
            $options = $this->reduceValueOptions($options);
            if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
                $default_value = [];
            }
        }
        if (empty($this->options['expose']['multiple'])) {
            if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
                $default_value = static::ALL_VALUE;
            }
            elseif (empty($default_value)) {
                $keys = array_keys($options);
                $default_value = array_shift($keys);
            }
            else {
                // Set the default value to be the first element of the array.
                $default_value = reset($default_value);
            }
        }
    }
    $referenced_type = $this->getReferencedEntityType();
    $form['value'] = [
        '#type' => 'select',
        '#title' => $this->t('Select @entity_types', [
            '@entity_types' => $referenced_type->getPluralLabel(),
        ]),
        '#multiple' => TRUE,
        '#options' => $options,
        // Set a minimum size to facilitate easier selection of entities.
'#size' => min(8, count($options)),
        '#default_value' => $default_value,
    ];
    $user_input = $form_state->getUserInput();
    if ($is_exposed && isset($identifier) && !isset($user_input[$identifier])) {
        $user_input[$identifier] = $default_value;
        $form_state->setUserInput($user_input);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.