diff --git a/src/Display/DisplayPluginBase.php b/src/Display/DisplayPluginBase.php index 444c7b2..9a95711 100644 --- a/src/Display/DisplayPluginBase.php +++ b/src/Display/DisplayPluginBase.php @@ -2,6 +2,8 @@ namespace Drupal\search_api\Display; +use Drupal\Core\Plugin\PluginBase; +use Drupal\search_api\Entity\Index; use Drupal\search_api\Plugin\IndexPluginBase; /** @@ -26,7 +28,7 @@ * @see \Drupal\search_api\Display\DisplayInterface * @see plugin_api */ -abstract class DisplayPluginBase extends IndexPluginBase implements DisplayInterface { +abstract class DisplayPluginBase extends PluginBase implements DisplayInterface { /** * {@inheritdoc} @@ -47,7 +49,8 @@ public function isRenderedInCurrentRequest() { * {@inheritdoc} */ public function getIndex() { - return $this->index; + $definition = $this->getPluginDefinition(); + return Index::load($definition['index']); } } diff --git a/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php index 1d3f65c..933867a 100644 --- a/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php +++ b/src/Plugin/search_api/display/ViewsPageDisplayDeriver.php @@ -5,6 +5,7 @@ use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Component\Plugin\PluginBase; use Drupal\search_api\Display\DisplayDeriverBase; +use Drupal\search_api\Plugin\views\query\SearchApiQuery; /** * Derives a display plugin definition for every Search API view. @@ -33,12 +34,19 @@ public function getDerivativeDefinitions($base_plugin_definition) { /** @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) { + $index = SearchApiQuery::getIndexFromTable($view->get('base_table')); + if ($index) { $displays = $view->get('display'); foreach ($displays as $name => $display_info) { if ($display_info['display_plugin'] == "page") { - $machine_name = $view->id() . PluginBase::DERIVATIVE_SEPARATOR . $name; + $machine_name = $base = $view->id() . '__' . $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 . '_' . ++$i; + } $label_arguments = array( '%view_name' => $view->label(), @@ -52,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, + 'index' => $index->id(), ) + $base_plugin_definition; $arguments = array( diff --git a/src/Tests/ViewsTest.php b/src/Tests/ViewsTest.php index 12c529b..d976b7f 100644 --- a/src/Tests/ViewsTest.php +++ b/src/Tests/ViewsTest.php @@ -3,6 +3,7 @@ namespace Drupal\search_api\Tests; use Drupal\Component\Utility\Html; +use Drupal\Core\Url; use Drupal\search_api\Entity\Index; use Drupal\search_api\Utility; @@ -148,6 +149,14 @@ public function testView() { 'keywords_op' => 'not empty', ); $this->checkResults($query, array(4), 'Search with multiple filters'); + + // Make sure there was a display plugin created for this view. + $displays = \Drupal::getContainer()->get('plugin.manager.search_api.display')->getInstances(); + $display_id = 'views_page:search_api_test_view__page_1'; + $this->assertEqual(array($display_id), array_keys($displays), 'A display plugin was created for the test view.'); + $view_url = Url::fromUserInput('/search-api-test')->toString(); + $this->assertEqual($view_url, $displays[$display_id]->getPath()->toString(), 'Display returns the correct path.'); + $this->assertEqual('database_search_index', $displays[$display_id]->getIndex()->id(), 'Display returns the correct search index.'); } /**