Lines: 84.75%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\VisitorsSiteSearch; | |
| 6 | ||
| 7 | use Drupal\Core\Entity\EntityTypeManagerInterface; | |
| 8 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
| 9 | use Drupal\views\Entity\View; | |
| 10 | use Drupal\visitors\Attribute\VisitorsSiteSearch; | |
| 11 | use Drupal\visitors\Helper\SiteSearchQuery; | |
| 12 | use Drupal\visitors\Plugin\VisitorsSiteSearchBase; | |
| 13 | use Drupal\visitors\SiteSearch\SiteSearchHit; | |
| 14 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 15 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 16 | ||
| 17 | /** | |
| 18 | * Detects a Search API view from its config. | |
| 19 | * | |
| 20 | * The view is not executed, and the Search API module is not required. | |
| 21 | * Detection reads the base table and the exposed fulltext filter. | |
| 22 | */ | |
| 23 | #[VisitorsSiteSearch( | |
| 24 | id: 'search_api', | |
| 25 | label: 'Search API', | |
| 26 | weight: 10, | |
| 27 | )] | |
| 28 | final class SearchApi extends VisitorsSiteSearchBase implements ContainerFactoryPluginInterface { | |
| 29 | ||
| 30 | /** | |
| 31 | * Constructs a Search API detector. | |
| 32 | * | |
| 33 | * @param array $configuration | |
| 34 | * A configuration array containing information about the plugin instance. | |
| 35 | * @param string $plugin_id | |
| 36 | * The plugin ID for the plugin instance. | |
| 37 | * @param mixed $plugin_definition | |
| 38 | * The plugin implementation definition. | |
| 39 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack | |
| 40 | * The request stack. | |
| 41 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | |
| 42 | * The entity type manager. | |
| 43 | */ | |
| 44 | public function __construct( | |
| 45 | array $configuration, | |
| 46 | $plugin_id, | |
| 47 | $plugin_definition, | |
| 48 | RequestStack $requestStack, | |
| 49 | protected EntityTypeManagerInterface $entityTypeManager, | |
| 50 | ) { | |
| 51 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition, $requestStack); |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * {@inheritdoc} | |
| 56 | */ | |
| 57 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { | |
| 58 | 11 | return new static( |
| 59 | 11 | $configuration, |
| 60 | 11 | $plugin_id, |
| 61 | 11 | $plugin_definition, |
| 62 | 11 | $container->get('request_stack'), |
| 63 | 11 | $container->get('entity_type.manager'), |
| 64 | 11 | ); |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * {@inheritdoc} | |
| 69 | */ | |
| 70 | public function applies(string $route, string $path, array $query): bool { | |
| 71 | 11 | return $this->keywordParameter($route) !== NULL; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * {@inheritdoc} | |
| 76 | */ | |
| 77 | public function parse(string $route, string $path, array $query): ?SiteSearchHit { | |
| 78 | 1 | $parameter = $this->keywordParameter($route); |
| 79 | 1 | if ($parameter === NULL) { |
| 80 | 0 | return NULL; |
| 81 | } | |
| 82 | 1 | $keyword = SiteSearchQuery::scalar($query[$parameter] ?? NULL); |
| 83 | 1 | if ($keyword === '') { |
| 84 | 0 | return NULL; |
| 85 | } | |
| 86 | ||
| 87 | 1 | return new SiteSearchHit( |
| 88 | 1 | $this->getPluginId(), |
| 89 | 1 | $keyword, |
| 90 | 1 | SiteSearchQuery::page($query), |
| 91 | 1 | SiteSearchQuery::canonical($query, [$parameter]), |
| 92 | 1 | ); |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Exposed fulltext parameter for a Search API view route. | |
| 97 | * | |
| 98 | * @param string $route | |
| 99 | * The Drupal route name. | |
| 100 | * | |
| 101 | * @return string|null | |
| 102 | * The query parameter, or NULL when this route is not a Search API view. | |
| 103 | */ | |
| 104 | private function keywordParameter(string $route): ?string { | |
| 105 | 11 | $ids = $this->viewIds($route); |
| 106 | 11 | if ($ids === NULL) { |
| 107 | 9 | return NULL; |
| 108 | } | |
| 109 | 2 | [$view_id, $display_id] = $ids; |
| 110 | 2 | $view = $this->entityTypeManager->getStorage('view')->load($view_id); |
| 111 | 2 | if (!$view instanceof View) { |
| 112 | 0 | return NULL; |
| 113 | } | |
| 114 | 2 | $base = (string) $view->get('base_table'); |
| 115 | 2 | if (!str_starts_with($base, 'search_api_index_')) { |
| 116 | 1 | return NULL; |
| 117 | } | |
| 118 | 1 | return $this->fulltextParameter($view, $display_id); |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Splits a view route into the view id and display id. | |
| 123 | * | |
| 124 | * @param string $route | |
| 125 | * The Drupal route name. | |
| 126 | * | |
| 127 | * @return array{0: string, 1: string}|null | |
| 128 | * The view id and display id, or NULL. | |
| 129 | */ | |
| 130 | private function viewIds(string $route): ?array { | |
| 131 | 11 | $parts = explode('.', $route, 3); |
| 132 | 11 | if (count($parts) !== 3 || $parts[0] !== 'view' || $parts[1] === '' || $parts[2] === '') { |
| 133 | 9 | return NULL; |
| 134 | } | |
| 135 | 2 | return [$parts[1], $parts[2]]; |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Identifier of the first exposed search_api_fulltext filter. | |
| 140 | * | |
| 141 | * @param \Drupal\views\Entity\View $view | |
| 142 | * The view config entity. | |
| 143 | * @param string $display_id | |
| 144 | * The display id. | |
| 145 | * | |
| 146 | * @return string|null | |
| 147 | * The exposed identifier, or NULL when the display has no fulltext filter. | |
| 148 | */ | |
| 149 | private function fulltextParameter(View $view, string $display_id): ?string { | |
| 150 | 1 | $display = $view->getDisplay($display_id); |
| 151 | 1 | if ($display === NULL) { |
| 152 | 0 | return NULL; |
| 153 | } | |
| 154 | 1 | $options = $display['display_options'] ?? []; |
| 155 | 1 | $uses_default = ($options['defaults']['filters'] ?? TRUE) !== FALSE; |
| 156 | 1 | if ($uses_default || !isset($options['filters'])) { |
| 157 | 1 | $default = $view->getDisplay('default'); |
| 158 | 1 | $options = $default['display_options'] ?? []; |
| 159 | } | |
| 160 | 1 | $filters = $options['filters'] ?? []; |
| 161 | 1 | if (!is_array($filters)) { |
| 162 | 0 | return NULL; |
| 163 | } | |
| 164 | 1 | foreach ($filters as $key => $filter) { |
| 165 | 1 | if (!is_array($filter)) { |
| 166 | 0 | continue; |
| 167 | } | |
| 168 | 1 | $plugin = (string) ($filter['plugin_id'] ?? $filter['id'] ?? ''); |
| 169 | 1 | if ($plugin !== 'search_api_fulltext') { |
| 170 | 0 | continue; |
| 171 | } | |
| 172 | 1 | $exposed = !empty($filter['exposed']) || !empty($filter['expose']['exposed']); |
| 173 | 1 | if (!$exposed) { |
| 174 | 0 | continue; |
| 175 | } | |
| 176 | 1 | $identifier = SiteSearchQuery::scalar($filter['expose']['identifier'] ?? $key); |
| 177 | 1 | return $identifier === '' ? NULL : $identifier; |
| 178 | } | |
| 179 | 0 | return NULL; |
| 180 | } | |
| 181 | ||
| 182 | } |