function ViewsSelection::buildConfigurationForm

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::buildConfigurationForm()
  2. 10 core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::buildConfigurationForm()
  3. 11.x core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::buildConfigurationForm()

Overrides SelectionPluginBase::buildConfigurationForm

File

core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php, line 136

Class

ViewsSelection
Plugin implementation of the 'selection' entity_reference.

Namespace

Drupal\views\Plugin\EntityReferenceSelection

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $view_settings = $this->getConfiguration()['view'];
    $displays = Views::getApplicableViews('entity_reference_display');
    // Filter views that list the entity type we want, and group the separate
    // displays by view.
    $entity_type = $this->entityTypeManager
        ->getDefinition($this->configuration['target_type']);
    $view_storage = $this->entityTypeManager
        ->getStorage('view');
    $options = [];
    foreach ($displays as $data) {
        list($view_id, $display_id) = $data;
        $view = $view_storage->load($view_id);
        if (in_array($view->get('base_table'), [
            $entity_type->getBaseTable(),
            $entity_type->getDataTable(),
        ])) {
            $display = $view->get('display');
            $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
        }
    }
    // The value of the 'view_and_display' select below will need to be split
    // into 'view_name' and 'view_display' in the final submitted values, so
    // we massage the data at validate time on the wrapping element (not
    // ideal).
    $form['view']['#element_validate'] = [
        [
            get_called_class(),
            'settingsFormValidate',
        ],
    ];
    if ($options) {
        $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
        $form['view']['view_and_display'] = [
            '#type' => 'select',
            '#title' => $this->t('View used to select the entities'),
            '#required' => TRUE,
            '#options' => $options,
            '#default_value' => $default,
            '#description' => '<p>' . $this->t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
        ];
        $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
        $form['view']['arguments'] = [
            '#type' => 'textfield',
            '#title' => $this->t('View arguments'),
            '#default_value' => $default,
            '#required' => FALSE,
            '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.'),
        ];
    }
    else {
        if ($this->currentUser
            ->hasPermission('administer views') && $this->moduleHandler
            ->moduleExists('views_ui')) {
            $form['view']['no_view_help'] = [
                '#markup' => '<p>' . $this->t('No eligible views were found. <a href=":create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href=":existing">existing view</a>.', [
                    ':create' => Url::fromRoute('views_ui.add')->toString(),
                    ':existing' => Url::fromRoute('entity.view.collection')->toString(),
                ]) . '</p>',
            ];
        }
        else {
            $form['view']['no_view_help']['#markup'] = '<p>' . $this->t('No eligible views were found.') . '</p>';
        }
    }
    return $form;
}

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