function DefaultSelection::buildConfigurationForm

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildConfigurationForm()
  2. 10 core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildConfigurationForm()
  3. 11.x core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::buildConfigurationForm()

Overrides SelectionPluginBase::buildConfigurationForm

3 calls to DefaultSelection::buildConfigurationForm()
TermSelection::buildConfigurationForm in core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php
Form constructor.
UserSelection::buildConfigurationForm in core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php
Form constructor.
WorkspaceSelection::buildConfigurationForm in core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php
Form constructor.
3 methods override DefaultSelection::buildConfigurationForm()
TermSelection::buildConfigurationForm in core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php
Form constructor.
UserSelection::buildConfigurationForm in core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php
Form constructor.
WorkspaceSelection::buildConfigurationForm in core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php
Form constructor.

File

core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php, line 157

Class

DefaultSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\Core\Entity\Plugin\EntityReferenceSelection

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $configuration = $this->getConfiguration();
    $entity_type_id = $configuration['target_type'];
    $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
    $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($entity_type_id);
    if ($entity_type->hasKey('bundle')) {
        $bundle_options = [];
        foreach ($bundles as $bundle_name => $bundle_info) {
            $bundle_options[$bundle_name] = $bundle_info['label'];
        }
        natsort($bundle_options);
        $selected_bundles = array_intersect_key($bundle_options, array_filter((array) $configuration['target_bundles']));
        $form['target_bundles'] = [
            '#type' => 'checkboxes',
            '#title' => $entity_type->getBundleLabel(),
            '#options' => $bundle_options,
            '#default_value' => (array) $configuration['target_bundles'],
            '#required' => TRUE,
            '#size' => 6,
            '#multiple' => TRUE,
            '#element_validate' => [
                [
                    static::class,
                    'elementValidateFilter',
                ],
            ],
            '#ajax' => TRUE,
            '#limit_validation_errors' => [],
        ];
        $form['target_bundles_update'] = [
            '#type' => 'submit',
            '#value' => $this->t('Update form'),
            '#limit_validation_errors' => [],
            '#attributes' => [
                'class' => [
                    'js-hide',
                ],
            ],
            '#submit' => [
                [
                    EntityReferenceItem::class,
                    'settingsAjaxSubmit',
                ],
            ],
        ];
    }
    else {
        $form['target_bundles'] = [
            '#type' => 'value',
            '#value' => [],
        ];
    }
    if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
        $options = $entity_type->hasKey('bundle') ? $selected_bundles : $bundles;
        $fields = [];
        foreach (array_keys($options) as $bundle) {
            $bundle_fields = array_filter($this->entityFieldManager
                ->getFieldDefinitions($entity_type_id, $bundle), function ($field_definition) {
                return !$field_definition->isComputed();
            });
            foreach ($bundle_fields as $field_name => $field_definition) {
                
                /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
                $columns = $field_definition->getFieldStorageDefinition()
                    ->getColumns();
                // If there is more than one column, display them all, otherwise just
                // display the field label.
                // @todo: Use property labels instead of the column name.
                if (count($columns) > 1) {
                    foreach ($columns as $column_name => $column_info) {
                        $fields[$field_name . '.' . $column_name] = $this->t('@label (@column)', [
                            '@label' => $field_definition->getLabel(),
                            '@column' => $column_name,
                        ]);
                    }
                }
                else {
                    $fields[$field_name] = $this->t('@label', [
                        '@label' => $field_definition->getLabel(),
                    ]);
                }
            }
        }
        $form['sort']['field'] = [
            '#type' => 'select',
            '#title' => $this->t('Sort by'),
            '#options' => $fields,
            '#ajax' => TRUE,
            '#empty_value' => '_none',
            '#sort_options' => TRUE,
            '#limit_validation_errors' => [],
            '#default_value' => $configuration['sort']['field'],
        ];
        if ($entity_type->hasKey('bundle')) {
            $form['sort']['field']['#states'] = [
                'visible' => [
                    ':input[name^="settings[handler_settings][target_bundles]["]' => [
                        'checked' => TRUE,
                    ],
                ],
            ];
        }
        $form['sort']['settings'] = [
            '#type' => 'container',
            '#attributes' => [
                'class' => [
                    'entity_reference-settings',
                ],
            ],
            '#process' => [
                [
                    EntityReferenceItem::class,
                    'formProcessMergeParent',
                ],
            ],
        ];
        $form['sort']['settings']['direction'] = [
            '#type' => 'select',
            '#title' => $this->t('Sort direction'),
            '#required' => TRUE,
            '#options' => [
                'ASC' => $this->t('Ascending'),
                'DESC' => $this->t('Descending'),
            ],
            '#default_value' => $configuration['sort']['direction'],
            '#states' => [
                'visible' => [
                    ':input[name="settings[handler_settings][sort][field]"]' => [
                        '!value' => '_none',
                    ],
                ],
            ],
        ];
        if ($entity_type->hasKey('bundle')) {
            $form['sort']['settings']['direction']['#states']['visible'][] = [
                ':input[name^="settings[handler_settings][target_bundles]["]' => [
                    'checked' => TRUE,
                ],
            ];
        }
    }
    $form['auto_create'] = [
        '#type' => 'checkbox',
        '#title' => $this->t("Create referenced entities if they don't already exist"),
        '#default_value' => $configuration['auto_create'],
        '#weight' => -2,
    ];
    if ($entity_type->hasKey('bundle')) {
        $form['auto_create_bundle'] = [
            '#type' => 'select',
            '#title' => $this->t('Store new items in'),
            '#options' => $selected_bundles,
            '#default_value' => $configuration['auto_create_bundle'],
            '#access' => count($selected_bundles) > 1,
            '#states' => [
                'visible' => [
                    ':input[name="settings[handler_settings][auto_create]"]' => [
                        'checked' => TRUE,
                    ],
                ],
            ],
            '#weight' => -1,
        ];
    }
    return $form;
}

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