diff --git a/core/modules/search/src/Annotation/SearchPlugin.php b/core/modules/search/src/Annotation/SearchPlugin.php index 38ff1f6c4bb1be047f4eb31f3391dd6f75e6565a..7be1a1da042ace7e0c28410cb562c9275f9411df 100644 --- a/core/modules/search/src/Annotation/SearchPlugin.php +++ b/core/modules/search/src/Annotation/SearchPlugin.php @@ -37,4 +37,11 @@ class SearchPlugin extends Plugin { */ public $title; + /** + * Whether or not search results should be displayed in admin theme. + * + * @var bool + */ + public $use_admin_theme = FALSE; + } diff --git a/core/modules/search/src/Plugin/SearchInterface.php b/core/modules/search/src/Plugin/SearchInterface.php index 09d1fff41d1a2eae74fbac3c3a3fae3b513cda9f..3dee690ff384151f720674bee503b1bc9831000d 100644 --- a/core/modules/search/src/Plugin/SearchInterface.php +++ b/core/modules/search/src/Plugin/SearchInterface.php @@ -145,4 +145,15 @@ public function searchFormAlter(array &$form, FormStateInterface $form_state); */ public function buildSearchUrlQuery(FormStateInterface $form_state); + /** + * Returns whether or not search results should be displayed in admin theme. + * + * @return bool + * TRUE if search results should be displayed in the admin theme, and FALSE + * otherwise. + * + * @see \Drupal\search\Annotation\SearchPlugin::$use_admin_theme + */ + public function usesAdminTheme(); + } diff --git a/core/modules/search/src/Plugin/SearchPluginBase.php b/core/modules/search/src/Plugin/SearchPluginBase.php index 7312df49fc17ec542a7a87c094f6addc0df86974..4c7d93335ea318c4c7357fc4948708411ee347b2 100644 --- a/core/modules/search/src/Plugin/SearchPluginBase.php +++ b/core/modules/search/src/Plugin/SearchPluginBase.php @@ -162,4 +162,11 @@ public function getHelp() { return $help; } + /** + * {@inheritdoc} + */ + public function usesAdminTheme() { + return $this->pluginDefinition['use_admin_theme']; + } + } diff --git a/core/modules/search/src/Routing/SearchPageRoutes.php b/core/modules/search/src/Routing/SearchPageRoutes.php index 6e199d3dfc94aeb73cbeff9f1b8cddf913ded904..30fd1af18f0f955b50800f1607ce4c5254e34368 100644 --- a/core/modules/search/src/Routing/SearchPageRoutes.php +++ b/core/modules/search/src/Routing/SearchPageRoutes.php @@ -110,6 +110,10 @@ public function routes() { ], ] ); + if ($entity->getPlugin()->usesAdminTheme()) { + $routes["search.view_$entity_id"]->setOption('_admin_route', TRUE); + $routes["search.help_$entity_id"]->setOption('_admin_route', TRUE); + } } return $routes; } diff --git a/core/modules/search/tests/modules/search_extra_type/src/Plugin/Search/SearchExtraTypeSearch.php b/core/modules/search/tests/modules/search_extra_type/src/Plugin/Search/SearchExtraTypeSearch.php index 6c2218695b9721b9d5498d9d3a0ebdf80325d0c9..f7c8599a6299484139926704ca415ca68f371175 100644 --- a/core/modules/search/tests/modules/search_extra_type/src/Plugin/Search/SearchExtraTypeSearch.php +++ b/core/modules/search/tests/modules/search_extra_type/src/Plugin/Search/SearchExtraTypeSearch.php @@ -12,7 +12,8 @@ * * @SearchPlugin( * id = "search_extra_type_search", - * title = @Translation("Dummy search type") + * title = @Translation("Dummy search type"), + * use_admin_theme = TRUE, * ) */ class SearchExtraTypeSearch extends ConfigurableSearchPluginBase { diff --git a/core/modules/search/tests/src/Functional/SearchAdminThemeTest.php b/core/modules/search/tests/src/Functional/SearchAdminThemeTest.php new file mode 100644 index 0000000000000000000000000000000000000000..aaaa4b3823178047e9b2ee2f69da2fcd1d23f428 --- /dev/null +++ b/core/modules/search/tests/src/Functional/SearchAdminThemeTest.php @@ -0,0 +1,105 @@ +<?php + +namespace Drupal\Tests\search\Functional; + +use Drupal\Tests\BrowserTestBase; + +/** + * Verify the search results using administration theme for specific plugins. + * + * @see \Drupal\search\Annotation\SearchPlugin::$use_admin_theme + * @see \Drupal\Tests\system\Functional\System\ThemeTest::testAdministrationTheme() + * + * @group search + */ +class SearchAdminThemeTest extends BrowserTestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['node', 'search', 'search_extra_type', 'user']; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * The administration theme name. + * + * @var string + */ + protected $adminTheme = 'seven'; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + // Install an administration theme to make sure it used for search results. + \Drupal::service('theme_installer')->install([$this->adminTheme]); + \Drupal::configFactory() + ->getEditable('system.theme') + ->set('admin', $this->adminTheme) + ->save(); + // Create searching user. + $user = $this->drupalCreateUser([ + 'access content', + 'search content', + 'access user profiles', + 'view the administration theme', + ]); + // Log in with sufficient privileges. + $this->drupalLogin($user); + } + + /** + * Tests that search results could be displayed in administration theme. + * + * @see \Drupal\node\Plugin\Search\NodeSearch + * @see \Drupal\search_extra_type\Plugin\Search\SearchExtraTypeSearch + * @see \Drupal\user\Plugin\Search\UserSearch + */ + public function testSearchUsingAdminTheme() { + /** @var \Drupal\search\SearchPageRepositoryInterface $repository */ + $repository = \Drupal::service('search.search_page_repository'); + $pages = $repository->getActiveSearchPages(); + // Test default configured pages. + $page_ids = [ + 'node_search' => FALSE, + 'dummy_search_type' => TRUE, + 'user_search' => FALSE, + ]; + foreach ($page_ids as $page_id => $use_admin_theme) { + $plugin = $pages[$page_id]->getPlugin(); + $path = 'search/' . $pages[$page_id]->getPath(); + $this->drupalGet($path); + $session = $this->assertSession(); + // Make sure help plugin rendered help link. + // @todo Test label in https://www.drupal.org/node/3086795 + $path_help = $path . '/help'; + $session->linkByHrefExists($path_help); + $this->assertSame($use_admin_theme, $plugin->usesAdminTheme()); + $this->assertAdminTheme($use_admin_theme); + $this->drupalGet($path_help); + $this->assertAdminTheme($use_admin_theme); + } + } + + /** + * Asserts whether an administrative theme's used for the loaded page. + * + * @param bool $is_admin + * TRUE to test for administrative theme, FALSE otherwise. + */ + protected function assertAdminTheme($is_admin) { + if ($is_admin) { + $this->assertRaw('core/themes/' . $this->adminTheme); + } + else { + $this->assertNoRaw('core/themes/' . $this->adminTheme); + } + } + +}