function UserSelection::buildConfigurationForm

Same name in other branches
  1. 9 core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildConfigurationForm()
  2. 8.9.x core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildConfigurationForm()
  3. 11.x core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php \Drupal\user\Plugin\EntityReferenceSelection\UserSelection::buildConfigurationForm()

Overrides DefaultSelection::buildConfigurationForm

File

core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php, line 104

Class

UserSelection
Provides specific access control for the user entity type.

Namespace

Drupal\user\Plugin\EntityReferenceSelection

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $configuration = $this->getConfiguration();
    $form['include_anonymous'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Include the anonymous user.'),
        '#default_value' => $configuration['include_anonymous'],
    ];
    // Add user specific filter options.
    $form['filter']['type'] = [
        '#type' => 'select',
        '#title' => $this->t('Filter by'),
        '#options' => [
            '_none' => $this->t('- None -'),
            'role' => $this->t('User role'),
        ],
        // Use a form process callback to build #ajax property properly and also
        // to avoid code duplication.
        // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsAjaxProcess()
'#ajax' => TRUE,
        '#limit_validation_errors' => [],
        '#default_value' => $configuration['filter']['type'],
    ];
    $form['filter']['settings'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'entity_reference-settings',
            ],
        ],
        '#process' => [
            [
                '\\Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem',
                'formProcessMergeParent',
            ],
        ],
    ];
    if ($configuration['filter']['type'] == 'role') {
        $roles = Role::loadMultiple();
        unset($roles[RoleInterface::ANONYMOUS_ID]);
        unset($roles[RoleInterface::AUTHENTICATED_ID]);
        $roles = array_map(fn(RoleInterface $role) => $role->label(), $roles);
        $form['filter']['settings']['role'] = [
            '#type' => 'checkboxes',
            '#title' => $this->t('Restrict to the selected roles'),
            '#required' => TRUE,
            '#options' => $roles,
            '#default_value' => $configuration['filter']['role'],
        ];
    }
    $form += parent::buildConfigurationForm($form, $form_state);
    return $form;
}

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