diff --git a/src/Display/DisplayInterface.php b/src/Display/DisplayInterface.php index 1a81b38..82a17ac 100644 --- a/src/Display/DisplayInterface.php +++ b/src/Display/DisplayInterface.php @@ -52,6 +52,10 @@ interface DisplayInterface extends PluginInspectionInterface, DerivativeInspecti /** * Returns the base path used by this display. * + * In general, \Drupal\search_api\Display\DisplayInterface::getUrl() should be + * preferred over this method. However, in situations where creating a URL + * object might cause errors, this is the safer method to use. + * * @return string|null * The base path for this display, or NULL if there is none. */ diff --git a/src/Display/DisplayPluginBase.php b/src/Display/DisplayPluginBase.php index cb06329..a8f451b 100644 --- a/src/Display/DisplayPluginBase.php +++ b/src/Display/DisplayPluginBase.php @@ -135,9 +135,8 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayInterface * {@inheritdoc} */ public function getUrl() { - $plugin_definition = $this->getPluginDefinition(); - if (!empty($plugin_definition['path'])) { - return Url::fromUserInput($plugin_definition['path']); + if ($path = $this->getPath()) { + return Url::fromUserInput($path); } return NULL; } @@ -149,7 +148,7 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayInterface $plugin_definition = $this->getPluginDefinition(); if (!empty($plugin_definition['path'])) { return $plugin_definition['path']; - } + } return NULL; } @@ -157,10 +156,9 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayInterface * {@inheritdoc} */ public function isRenderedInCurrentRequest() { - $plugin_definition = $this->getPluginDefinition(); - if (!empty($plugin_definition['path'])) { + if ($path = $this->getPath()) { $current_path = $this->getCurrentPath()->getPath(); - return $current_path == $plugin_definition['path']; + return $current_path == $path; } return FALSE; } diff --git a/src/Plugin/search_api/display/ViewsBlock.php b/src/Plugin/search_api/display/ViewsBlock.php index acfa87b..49c90ea 100644 --- a/src/Plugin/search_api/display/ViewsBlock.php +++ b/src/Plugin/search_api/display/ViewsBlock.php @@ -16,14 +16,6 @@ class ViewsBlock extends ViewsDisplayBase { /** * {@inheritdoc} */ - public function getUrl() { - // Blocks don't have a path, so don't return one. - return NULL; - } - - /** - * {@inheritdoc} - */ public function isRenderedInCurrentRequest() { // There is no way to know if a block is embedded on a page, because // blocks can be rendered in isolation (see big_pipe, esi, ...). To be diff --git a/tests/src/Functional/ViewsTest.php b/tests/src/Functional/ViewsTest.php index 2d16743..5bcddde 100644 --- a/tests/src/Functional/ViewsTest.php +++ b/tests/src/Functional/ViewsTest.php @@ -13,8 +13,8 @@ use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Drupal\entity_test\Entity\EntityTestMulRevChanged; use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\search_api\Display\DisplayInterface; use Drupal\search_api\Entity\Index; -use Drupal\search_api\SearchApiException; use Drupal\search_api\Utility\Utility; /** @@ -283,6 +283,7 @@ class ViewsTest extends SearchApiBrowserTestBase { $this->checkResults($query, [], 'Search for results of no available datasource'); // Make sure there was a display plugin created for this view. + /** @var \Drupal\search_api\Display\DisplayInterface[] $displays */ $displays = \Drupal::getContainer() ->get('plugin.manager.search_api.display') ->getInstances(); @@ -293,7 +294,7 @@ class ViewsTest extends SearchApiBrowserTestBase { $this->assertArrayHasKey('views_rest:search_api_test_view__rest_export_1', $displays, 'A display plugin was created for the test view block display.'); $this->assertEquals('/search-api-test', $displays[$display_id]->getPath(), 'Display returns the correct path.'); $view_url = Url::fromUserInput('/search-api-test')->toString(); - $this->assertEquals($view_url, $displays[$display_id]->getUrl()->toString(), 'Display returns the correct path.'); + $this->assertEquals($view_url, $displays[$display_id]->getUrl()->toString(), 'Display returns the correct URL.'); $this->assertNull($displays['views_block:search_api_test_view__block_1']->getPath(), 'Block display returns the correct path.'); $this->assertEquals('/search-api-rest-test', $displays['views_rest:search_api_test_view__rest_export_1']->getPath(), 'REST display returns the correct path.');