function ViewsSelection::initializeView

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

Initializes a view.

Parameters

string|null $match: (Optional) Text to match the label against. Defaults to NULL.

string $match_operator: (Optional) The operation the matching should be done with. Defaults to "CONTAINS".

int $limit: Limit the query to a given number of items. Defaults to 0, which indicates no limiting.

array|null $ids: Array of entity IDs. Defaults to NULL.

Return value

bool Return TRUE if the view was initialized, FALSE otherwise.

1 call to ViewsSelection::initializeView()
ViewsSelection::getDisplayExecutionResults in core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
Fetches the results of executing the display.

File

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

Class

ViewsSelection
Plugin implementation of the 'selection' entity_reference.

Namespace

Drupal\views\Plugin\EntityReferenceSelection

Code

protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
    $view_name = $this->getConfiguration()['view']['view_name'];
    $display_name = $this->getConfiguration()['view']['display_name'];
    // Check that the view is valid and the display still exists.
    $this->view = Views::getView($view_name);
    if (!$this->view || !$this->view
        ->access($display_name)) {
        \Drupal::messenger()->addWarning($this->t('The reference view %view_name cannot be found.', [
            '%view_name' => $view_name,
        ]));
        return FALSE;
    }
    $this->view
        ->setDisplay($display_name);
    // Pass options to the display handler to make them available later.
    $entity_reference_options = [
        'match' => $match,
        'match_operator' => $match_operator,
        'limit' => $limit,
        'ids' => $ids,
    ];
    $this->view->displayHandlers
        ->get($display_name)
        ->setOption('entity_reference_options', $entity_reference_options);
    return TRUE;
}

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