diff --git a/search_api.plugin_type.yml b/search_api.plugin_type.yml index c1b49c7..e2607e9 100644 --- a/search_api.plugin_type.yml +++ b/search_api.plugin_type.yml @@ -17,3 +17,8 @@ search_api_tracker: label: Search API tracker plugin_manager_service_id: plugin.manager.search_api.tracker plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator + +search_api_display: + label: Search API display + plugin_manager_service_id: plugin.manager.search_api.display + plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator diff --git a/search_api.services.yml b/search_api.services.yml index 6c1207a..9a42b1c 100644 --- a/search_api.services.yml +++ b/search_api.services.yml @@ -26,6 +26,10 @@ services: class: Drupal\search_api\Tracker\TrackerPluginManager parent: default_plugin_manager + plugin.manager.search_api.display: + class: Drupal\search_api\Display\DisplayPluginManager + parent: default_plugin_manager + search_api.index_task_manager: class: Drupal\search_api\Task\IndexTaskManager arguments: ['@state', '@entity_type.manager'] diff --git a/src/Annotation/SearchApiDisplay.php b/src/Annotation/SearchApiDisplay.php new file mode 100644 index 0000000..af4fc24 --- /dev/null +++ b/src/Annotation/SearchApiDisplay.php @@ -0,0 +1,49 @@ +get('entity_type.manager'); + $deriver->setEntityTypeManager($entity_type_manager); + + /** @var \Drupal\Core\StringTranslation\TranslationInterface $translation */ + $translation = $container->get('string_translation'); + $deriver->setStringTranslation($translation); + + return $deriver; + } + + /** + * Retrieves the entity manager. + * + * @return \Drupal\Core\Entity\EntityTypeManager + * The entity manager. + */ + public function getEntityTypeManager() { + return $this->entityTypeManager ?: \Drupal::service('entity_type.manager'); + } + + /** + * Sets the entity manager. + * + * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager + * The entity manager. + * + * @return $this + */ + public function setEntityTypeManager(EntityTypeManager $entity_type_manager) { + $this->entityTypeManager = $entity_type_manager; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinition($derivative_id, $base_plugin_definition) { + $derivatives = $this->getDerivativeDefinitions($base_plugin_definition); + return isset($derivatives[$derivative_id]) ? $derivatives[$derivative_id] : NULL; + } + + /** + * Compares two plugin definitions according to their labels. + * + * @param array $a + * A plugin definition, with at least a "label" key. + * @param array $b + * Another plugin definition. + * + * @return int + * An integer less than, equal to, or greater than zero if the first + * argument is considered to be respectively less than, equal to, or greater + * than the second. + */ + public function compareDerivatives(array $a, array $b) { + return strnatcasecmp($a['label'], $b['label']); + } + +} diff --git a/src/Display/DisplayInterface.php b/src/Display/DisplayInterface.php new file mode 100644 index 0000000..f94cfcc --- /dev/null +++ b/src/Display/DisplayInterface.php @@ -0,0 +1,55 @@ +getPluginDefinition(); + return $plugin_definition['label']; + } + + /** + * {@inheritdoc} + */ + public function isRenderedInCurrentRequest() { + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function getIndex() { + return $this->index; + } + +} diff --git a/src/Display/DisplayPluginManager.php b/src/Display/DisplayPluginManager.php new file mode 100644 index 0000000..4fd8c2e --- /dev/null +++ b/src/Display/DisplayPluginManager.php @@ -0,0 +1,40 @@ +setCacheBackend($cache_backend, 'search_api_displays'); + } + +} diff --git a/src/Plugin/search_api/display/ViewsPageDisplay.php b/src/Plugin/search_api/display/ViewsPageDisplay.php new file mode 100644 index 0000000..7f58649 --- /dev/null +++ b/src/Plugin/search_api/display/ViewsPageDisplay.php @@ -0,0 +1,31 @@ +pluginDefinition['view_id']); + $view->setDisplay($this->pluginDefinition['view_display']); + return $view->getDisplay()->getPath(); + } + +} diff --git a/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php new file mode 100644 index 0000000..6d174e7 --- /dev/null +++ b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php @@ -0,0 +1,71 @@ +entityTypeManager->getStorage('view'); + $all_views = $views_storage->loadMultiple(); + } + catch (PluginNotFoundException $e) { + return []; + } + + if (!isset($this->derivatives[$base_plugin_id])) { + $plugin_derivatives = array(); + + /** @var \Drupal\views\Entity\View $view */ + foreach ($all_views as $view) { + // Hardcoded usage of Search API views, for now. + if (strpos($view->get('base_table'), 'search_api_index') !== FALSE) { + $displays = $view->get('display'); + foreach ($displays as $name => $display_info) { + if ($display_info['display_plugin'] == "page") { + $machine_name = $view->id() . PluginBase::DERIVATIVE_SEPARATOR . $name; + + $plugin_derivatives[$machine_name] = [ + 'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name, + 'label' => $this->t('View name: %view_name. Display: %display_title', ['%view_name' => $view->label(), '%display_title' => $display_info['display_title']]), + 'description' => $this->t('Provides a display.'), + 'view_id' => $view->id(), + 'view_display' => $name, + ] + $base_plugin_definition; + + $sources[] = $this->t( + 'View name: %view. Display: %display', + ['%view' => $view->label(), '%display' => $display_info['display_title']] + ); + } + } + } + } + uasort($plugin_derivatives, array($this, 'compareDerivatives')); + + $this->derivatives[$base_plugin_id] = $plugin_derivatives; + } + return $this->derivatives[$base_plugin_id]; + } + +}