Same name in other branches
  1. 8.x-2.x src/FieldStateBase.php \Drupal\field_states_ui\FieldStateBase::buildConfigurationForm()

Fichier

src/FieldStateBase.php, line 189

Classe

FieldStateBase
Provides a base class for field staes.

Namespace

Drupal\field_states_ui

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $display = $form_state->getFormObject()
        ->getEntity();
    $fields = [];
    $definitions = \Drupal::entityManager()->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle());
    $current_field = $form_state->get('field_states_ui_edit');
    foreach ($display->getComponents() as $name => $field) {
        if (!isset($definitions[$name]) || $name === $current_field) {
            continue;
        }
        $fields[$name] = $definitions[$name]->getLabel();
    }
    asort($fields, SORT_NATURAL | SORT_FLAG_CASE);
    $form['target'] = [
        '#type' => 'select',
        '#title' => t('Target'),
        '#description' => t('The field to run a comparison on'),
        '#required' => TRUE,
        '#other' => t('Other element on the page'),
        '#other_description' => t('Should be a valid jQuery style element selector.'),
        '#options' => $fields,
        '#default_value' => isset($this->configuration['target']) ? $this->configuration['target'] : '',
    ];
    $form['comparison'] = [
        '#type' => 'select',
        '#title' => t('Comparison Type'),
        '#options' => [
            'empty' => 'empty',
            'filled' => 'filled',
            'checked' => 'checked',
            'unchecked' => 'unchecked',
            'expanded' => 'expanded',
            'collapsed' => 'collapsed',
            'value' => 'value',
        ],
        '#default_value' => isset($this->configuration['comparison']) ? $this->configuration['comparison'] : '',
    ];
    $form['value'] = [
        '#type' => 'textfield',
        '#title' => t('Value'),
        '#default_value' => isset($this->configuration['value']) ? $this->configuration['value'] : '',
        '#states' => [
            'visible' => [
                'select[name$="[comparison]"]' => [
                    'value' => 'value',
                ],
            ],
        ],
    ];
    return $form;
}