ViewsDisplayDeriver.php in Search API 8
File
src/Plugin/search_api/display/ViewsDisplayDeriver.php
View source
<?php
namespace Drupal\search_api\Plugin\search_api\display;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\search_api\Display\DisplayDeriverBase;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\views\ViewEntityInterface;
class ViewsDisplayDeriver extends DisplayDeriverBase {
public function getDerivativeDefinitions($base_plugin_definition) {
if (!isset($this->derivatives)) {
$this->derivatives = [];
try {
$views_storage = $this->entityTypeManager
->getStorage('view');
$all_views = $views_storage
->loadMultiple();
} catch (PluginNotFoundException $e) {
return $this->derivatives;
}
foreach ($all_views as $view) {
$this->derivatives += $this
->getDisplaysForView($base_plugin_definition, $view, $this->derivatives);
}
}
return $this->derivatives;
}
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) {
$base_machine_name = $view
->id() . '__' . $name;
$machine_name = $base_machine_name;
$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();
$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;
if ($display
->hasPath()) {
$plugin_derivatives[$machine_name]['path'] = '/' . $display
->getPath();
}
}
}
return $plugin_derivatives;
}
}