diff --git a/src/Display/DisplayInterface.php b/src/Display/DisplayInterface.php index f7da4c3..f5a0f24 100644 --- a/src/Display/DisplayInterface.php +++ b/src/Display/DisplayInterface.php @@ -4,6 +4,7 @@ use Drupal\Component\Plugin\DerivativeInspectionInterface; use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; /** * Defines an interface for display plugins. @@ -13,7 +14,7 @@ * @see \Drupal\search_api\Display\DisplayPluginBase * @see plugin_api */ -interface DisplayInterface extends PluginInspectionInterface, DerivativeInspectionInterface { +interface DisplayInterface extends PluginInspectionInterface, DerivativeInspectionInterface, ContainerFactoryPluginInterface { /** * Returns the display label. diff --git a/src/Display/DisplayPluginBase.php b/src/Display/DisplayPluginBase.php index fd5de36..d4c1a72 100644 --- a/src/Display/DisplayPluginBase.php +++ b/src/Display/DisplayPluginBase.php @@ -2,8 +2,11 @@ namespace Drupal\search_api\Display; +use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Plugin\PluginBase; +use Drupal\Core\Url; use Drupal\search_api\Entity\Index; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Defines a base class from which other display classes may extend. @@ -19,6 +22,7 @@ * @SearchApiDisplay( * id = "my_display", * label = @Translation("My display"), + * description = @Translation("A few words about this search display"), * ) * @endcode * @@ -30,6 +34,47 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayInterface { /** + * The current path service. + * + * @var CurrentPathStack|null + */ + protected $currentPath; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + $display = new static($configuration, $plugin_id, $plugin_definition); + + $display->setCurrentPath($container->get('path.current')); + + return $display; + } + + /** + * Retrieves the current path service. + * + * @return CurrentPathStack + * The current path service. + */ + public function getCurrentPath() { + return $this->currentPath ?: \Drupal::service('path.current'); + } + + /** + * Sets the current path service. + * + * @param \Drupal\Core\Path\CurrentPathStack $current_path + * The new current path service. + * + * @return $this + */ + public function setCurrentPath(CurrentPathStack $current_path) { + $this->currentPath = $current_path; + return $this; + } + + /** * {@inheritdoc} */ public function label() { @@ -48,16 +93,29 @@ public function getDescription() { /** * {@inheritdoc} */ - public function isRenderedInCurrentRequest() { - return FALSE; + public function getIndex() { + $plugin_definition = $this->getPluginDefinition(); + return Index::load($plugin_definition['index']); } /** * {@inheritdoc} */ - public function getIndex() { + public function getPath() { $plugin_definition = $this->getPluginDefinition(); - return Index::load($plugin_definition['index']); + return Url::fromUserInput($plugin_definition['path']); + } + + /** + * {@inheritdoc} + */ + public function isRenderedInCurrentRequest() { + $plugin_definition = $this->getPluginDefinition(); + if (isset($plugin_definition['path'])) { + $current_path = $this->getCurrentPath()->getPath(); + return $current_path == $plugin_definition['path']; + } + return FALSE; } } diff --git a/src/Plugin/search_api/display/ViewsPageDisplay.php b/src/Plugin/search_api/display/ViewsPageDisplay.php index f1fcba9..44ec510 100644 --- a/src/Plugin/search_api/display/ViewsPageDisplay.php +++ b/src/Plugin/search_api/display/ViewsPageDisplay.php @@ -2,9 +2,7 @@ namespace Drupal\search_api\Plugin\search_api\display; -use Drupal\Core\Url; use Drupal\search_api\Display\DisplayPluginBase; -use Drupal\views\Views; /** * Represents a views page display. @@ -14,33 +12,4 @@ * deriver = "Drupal\search_api\Plugin\search_api\display\ViewsPageDisplayDeriver" * ) */ -class ViewsPageDisplay extends DisplayPluginBase { - - /** - * {@inheritdoc} - */ - public function getPath() { - return Url::fromUserInput($this->getViewsPagePath()); - } - - /** - * {@inheritdoc} - */ - public function isRenderedInCurrentRequest() { - $current_path = \Drupal::service('path.current')->getPath(); - return $current_path == $this->getViewsPagePath(); - } - - /** - * Returns the page path for this Views page display. - * - * @return string - * The display's base path, with a leading slash (/). - */ - protected function getViewsPagePath(){ - $view = Views::getView($this->pluginDefinition['view_id']); - $view->setDisplay($this->pluginDefinition['view_display']); - return '/' . $view->getDisplay()->getPath(); - } - -} +class ViewsPageDisplay extends DisplayPluginBase {} diff --git a/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php index 97200f0..ee7af70 100644 --- a/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php +++ b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php @@ -60,6 +60,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { 'description' => $view->get('description') ? $this->t('%view_description - Represents the page display %display_title of view %view_name.', array('%view_name' => $view->label(), '%view_description' => $view->get('description'), '%display_title' => $display_info['display_title'])) : $this->t('Represents the page display %display_title of view %view_name.', array('%view_name' => $view->label(), '%display_title' => $display_info['display_title'])), 'view_id' => $view->id(), 'view_display' => $name, + 'path' => $view->getDisplay($name)->getPath(), 'index' => $index->id(), ) + $base_plugin_definition;