Issue #3421013 by mohit_aghera, sorlov, smustgrave, mstrelan, larowlan, catch: Convert Search plugin discovery to attributes

merge-requests/6972/head
Alex Pott 2024-03-08 09:31:04 +00:00
parent cb857b7884
commit 1e1dc945bb
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
6 changed files with 68 additions and 23 deletions

View File

@ -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 {
/**

View File

@ -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 {
/**

View File

@ -0,0 +1,40 @@
<?php
namespace Drupal\search\Attribute;
use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Defines a Search type attribute for plugin discovery.
*
* Search classes define search types for the core Search module. Each search
* type can be used to create search pages from the Search settings page.
*
* @see SearchPluginBase
*
* @ingroup search
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Search extends Plugin {
/**
* Constructs a Search attribute.
*
* @param string $id
* The plugin ID.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title
* The title for the search page tab.
* @param bool $use_admin_theme
* Whether search results should be displayed in admin theme or not.
* @param class-string|null $deriver
* (optional) The deriver class.
*/
public function __construct(
public readonly string $id,
public readonly ?TranslatableMarkup $title = NULL,
public readonly bool $use_admin_theme = FALSE,
public readonly ?string $deriver = NULL
) {}
}

View File

@ -5,6 +5,7 @@ namespace Drupal\search;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\search\Attribute\Search;
/**
* SearchExecute plugin manager.
@ -23,7 +24,7 @@ class SearchPluginManager extends DefaultPluginManager {
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Search', $namespaces, $module_handler, 'Drupal\search\Plugin\SearchInterface', 'Drupal\search\Annotation\SearchPlugin');
parent::__construct('Plugin/Search', $namespaces, $module_handler, 'Drupal\search\Plugin\SearchInterface', Search::class, 'Drupal\search\Annotation\SearchPlugin');
$this->setCacheBackend($cache_backend, 'search_plugins');
$this->alterInfo('search_plugin');
}

View File

@ -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 {
/**

View File

@ -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 {
/**