You are here

public function ViewsSelection::buildConfigurationForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides SelectionPluginBase::buildConfigurationForm

File

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

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'] = [
    [
      static::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;
}