From 1e1dc945bb1ef1a1e7eadc6af590dfcb9535fa59 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 8 Mar 2024 09:31:04 +0000 Subject: [PATCH] Issue #3421013 by mohit_aghera, sorlov, smustgrave, mstrelan, larowlan, catch: Convert Search plugin discovery to attributes --- .../help/src/Plugin/Search/HelpSearch.php | 13 +++--- .../node/src/Plugin/Search/NodeSearch.php | 11 ++--- core/modules/search/src/Attribute/Search.php | 40 +++++++++++++++++++ .../search/src/SearchPluginManager.php | 3 +- .../Plugin/Search/SearchExtraTypeSearch.php | 13 +++--- .../user/src/Plugin/Search/UserSearch.php | 11 ++--- 6 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 core/modules/search/src/Attribute/Search.php diff --git a/core/modules/help/src/Plugin/Search/HelpSearch.php b/core/modules/help/src/Plugin/Search/HelpSearch.php index e59d67549d9..3d04ee86357 100644 --- a/core/modules/help/src/Plugin/Search/HelpSearch.php +++ b/core/modules/help/src/Plugin/Search/HelpSearch.php @@ -13,8 +13,10 @@ use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\State\StateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\help\HelpSectionManager; use Drupal\help\SearchableHelpInterface; +use Drupal\search\Attribute\Search; use Drupal\search\Plugin\SearchIndexingInterface; use Drupal\search\Plugin\SearchPluginBase; use Drupal\search\SearchIndexInterface; @@ -30,15 +32,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * @see \Drupal\help\HelpSearchInterface * @see \Drupal\help\HelpSectionPluginInterface * - * @SearchPlugin( - * id = "help_search", - * title = @Translation("Help"), - * use_admin_theme = TRUE, - * ) - * * @internal * Plugin classes are internal. */ +#[Search( + id: 'help_search', + title: new TranslatableMarkup('Help'), + use_admin_theme: TRUE, +)] class HelpSearch extends SearchPluginBase implements AccessibleInterface, SearchIndexingInterface { /** diff --git a/core/modules/node/src/Plugin/Search/NodeSearch.php b/core/modules/node/src/Plugin/Search/NodeSearch.php index 88be7251400..734381a2a77 100644 --- a/core/modules/node/src/Plugin/Search/NodeSearch.php +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php @@ -19,7 +19,9 @@ use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Security\TrustedCallbackInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\node\NodeInterface; +use Drupal\search\Attribute\Search; use Drupal\search\Plugin\ConfigurableSearchPluginBase; use Drupal\search\Plugin\SearchIndexingInterface; use Drupal\search\SearchIndexInterface; @@ -28,12 +30,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Handles searching for node entities using the Search module index. - * - * @SearchPlugin( - * id = "node_search", - * title = @Translation("Content") - * ) */ +#[Search( + id: 'node_search', + title: new TranslatableMarkup('Content'), +)] class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface, TrustedCallbackInterface { /** diff --git a/core/modules/search/src/Attribute/Search.php b/core/modules/search/src/Attribute/Search.php new file mode 100644 index 00000000000..efe805c76be --- /dev/null +++ b/core/modules/search/src/Attribute/Search.php @@ -0,0 +1,40 @@ +setCacheBackend($cache_backend, 'search_plugins'); $this->alterInfo('search_plugin'); } 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 5726e247f65..c13643f3a08 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 @@ -4,18 +4,19 @@ namespace Drupal\search_extra_type\Plugin\Search; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Url; +use Drupal\search\Attribute\Search; use Drupal\search\Plugin\ConfigurableSearchPluginBase; /** * Executes a dummy keyword search. - * - * @SearchPlugin( - * id = "search_extra_type_search", - * title = @Translation("Dummy search type"), - * use_admin_theme = TRUE, - * ) */ +#[Search( + id: 'search_extra_type_search', + title: new TranslatableMarkup('Dummy search type'), + use_admin_theme: TRUE, +)] class SearchExtraTypeSearch extends ConfigurableSearchPluginBase { /** diff --git a/core/modules/user/src/Plugin/Search/UserSearch.php b/core/modules/user/src/Plugin/Search/UserSearch.php index 28f5d0e8336..c5d99b156a5 100644 --- a/core/modules/user/src/Plugin/Search/UserSearch.php +++ b/core/modules/user/src/Plugin/Search/UserSearch.php @@ -9,17 +9,18 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessibleInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; +use Drupal\search\Attribute\Search; use Drupal\search\Plugin\SearchPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Executes a keyword search for users against the {users} database table. - * - * @SearchPlugin( - * id = "user_search", - * title = @Translation("Users") - * ) */ +#[Search( + id: 'user_search', + title: new TranslatableMarkup('Users'), +)] class UserSearch extends SearchPluginBase implements AccessibleInterface { /**