You are here

protected function ViewsDisplayDeriver::getDisplaysForView in Search API 8

Creates derived plugin definitions for a view.

Parameters

array $base_plugin_definition: The plugin definition for this plugin.

\Drupal\views\ViewEntityInterface $view: The view to create plugin definitions for.

array $plugin_derivatives: An array of already existing derived plugin definitions.

Return value

array Returns an array of plugin definitions, keyed by derivative ID.

1 call to ViewsDisplayDeriver::getDisplaysForView()
ViewsDisplayDeriver::getDerivativeDefinitions in src/Plugin/search_api/display/ViewsDisplayDeriver.php
Gets the definition of all derivatives of a base plugin.

File

src/Plugin/search_api/display/ViewsDisplayDeriver.php, line 58

Class

ViewsDisplayDeriver
Derives a display plugin definition for all supported search view displays.

Namespace

Drupal\search_api\Plugin\search_api\display

Code

protected function getDisplaysForView(array $base_plugin_definition, ViewEntityInterface $view, array $plugin_derivatives) {
  $type = $base_plugin_definition['views_display_type'];
  $index = SearchApiQuery::getIndexFromTable($view
    ->get('base_table'));
  if (!$index instanceof IndexInterface) {
    return [];
  }
  $displays = $view
    ->get('display');
  foreach ($displays as $name => $display_info) {
    if ($display_info['display_plugin'] == $type) {

      // Create a machine name by getting the view ID and appending the name
      // of the display to it (block1, rest_export, foobar).
      $base_machine_name = $view
        ->id() . '__' . $name;
      $machine_name = $base_machine_name;

      // Make sure the machine name is unique. (Will almost always be
      // the case, unless a view or page ID contains two consecutive
      // underscores.)
      $i = 0;
      while (isset($plugin_derivatives[$machine_name])) {
        $machine_name = $base_machine_name . '_' . ++$i;
      }
      $label_arguments = [
        '%view_name' => $view
          ->label(),
        '%display_title' => $display_info['display_title'],
      ];
      $label = $this
        ->t('View %view_name, display %display_title', $label_arguments);
      $executable = $view
        ->getExecutable();
      $executable
        ->setDisplay($name);
      $display = $executable
        ->getDisplay();

      // Create the actual derivative plugin definition.
      $args = [
        '%view_name' => $view
          ->label(),
        '%display_title' => $display_info['display_title'],
      ];
      if ($view
        ->get('description')) {
        $args['%view_description'] = $view
          ->get('description');
        $description = $this
          ->t('%view_description – Represents the display %display_title of view %view_name.', $args);
      }
      else {
        $description = $this
          ->t('Represents the display %display_title of view %view_name.', $args);
      }
      $plugin_derivatives[$machine_name] = [
        'label' => $label,
        'description' => $description,
        'view_id' => $view
          ->id(),
        'view_display' => $name,
        'index' => $index
          ->id(),
      ] + $base_plugin_definition;

      // Add the path information to the definition.
      if ($display
        ->hasPath()) {
        $plugin_derivatives[$machine_name]['path'] = '/' . $display
          ->getPath();
      }
    }
  }
  return $plugin_derivatives;
}