Lines: 89.19%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Plugin\VisitorsSiteSearch; | |
| 6 | ||
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
| 9 | use Drupal\visitors\Attribute\VisitorsSiteSearch; | |
| 10 | use Drupal\visitors\Helper\SiteSearchQuery; | |
| 11 | use Drupal\visitors\Plugin\VisitorsSiteSearchBase; | |
| 12 | use Drupal\visitors\SiteSearch\SiteSearchHit; | |
| 13 | use Symfony\Component\DependencyInjection\ContainerInterface; | |
| 14 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 15 | ||
| 16 | /** | |
| 17 | * Detects a view display a site builder opted in. | |
| 18 | * | |
| 19 | * A view is not a search until it is listed in visitors.settings | |
| 20 | * site_search_views. | |
| 21 | */ | |
| 22 | #[VisitorsSiteSearch( | |
| 23 | id: 'view', | |
| 24 | label: 'View', | |
| 25 | weight: 20, | |
| 26 | )] | |
| 27 | final class ViewDisplay extends VisitorsSiteSearchBase implements ContainerFactoryPluginInterface { | |
| 28 | ||
| 29 | /** | |
| 30 | * Constructs a view display detector. | |
| 31 | * | |
| 32 | * @param array $configuration | |
| 33 | * A configuration array containing information about the plugin instance. | |
| 34 | * @param string $plugin_id | |
| 35 | * The plugin ID for the plugin instance. | |
| 36 | * @param mixed $plugin_definition | |
| 37 | * The plugin implementation definition. | |
| 38 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack | |
| 39 | * The request stack. | |
| 40 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 41 | * The config factory. | |
| 42 | */ | |
| 43 | public function __construct( | |
| 44 | array $configuration, | |
| 45 | $plugin_id, | |
| 46 | $plugin_definition, | |
| 47 | RequestStack $requestStack, | |
| 48 | protected ConfigFactoryInterface $configFactory, | |
| 49 | ) { | |
| 50 | 11 | parent::__construct($configuration, $plugin_id, $plugin_definition, $requestStack); |
| 51 | } | |
| 52 | ||
| 53 | /** | |
| 54 | * {@inheritdoc} | |
| 55 | */ | |
| 56 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static { | |
| 57 | 11 | return new static( |
| 58 | 11 | $configuration, |
| 59 | 11 | $plugin_id, |
| 60 | 11 | $plugin_definition, |
| 61 | 11 | $container->get('request_stack'), |
| 62 | 11 | $container->get('config.factory'), |
| 63 | 11 | ); |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * {@inheritdoc} | |
| 68 | */ | |
| 69 | public function applies(string $route, string $path, array $query): bool { | |
| 70 | 10 | return $this->keywordParameter($route) !== NULL; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * {@inheritdoc} | |
| 75 | */ | |
| 76 | public function parse(string $route, string $path, array $query): ?SiteSearchHit { | |
| 77 | 1 | $parameter = $this->keywordParameter($route); |
| 78 | 1 | if ($parameter === NULL) { |
| 79 | 0 | return NULL; |
| 80 | } | |
| 81 | 1 | $keyword = SiteSearchQuery::scalar($query[$parameter] ?? NULL); |
| 82 | 1 | if ($keyword === '') { |
| 83 | 1 | return NULL; |
| 84 | } | |
| 85 | ||
| 86 | 1 | return new SiteSearchHit( |
| 87 | 1 | $this->getPluginId(), |
| 88 | 1 | $keyword, |
| 89 | 1 | SiteSearchQuery::page($query), |
| 90 | 1 | SiteSearchQuery::canonical($query, [$parameter]), |
| 91 | 1 | ); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * Keyword parameter configured for this view route. | |
| 96 | * | |
| 97 | * @param string $route | |
| 98 | * The Drupal route name. | |
| 99 | * | |
| 100 | * @return string|null | |
| 101 | * The query parameter, or NULL when the display is not opted in. | |
| 102 | */ | |
| 103 | private function keywordParameter(string $route): ?string { | |
| 104 | 10 | $parts = explode('.', $route, 3); |
| 105 | 10 | if (count($parts) !== 3 || $parts[0] !== 'view' || $parts[1] === '' || $parts[2] === '') { |
| 106 | 9 | return NULL; |
| 107 | } | |
| 108 | 1 | $items = $this->configFactory->get('visitors.settings')->get('site_search_views'); |
| 109 | 1 | if (!is_array($items)) { |
| 110 | 0 | return NULL; |
| 111 | } | |
| 112 | 1 | foreach ($items as $item) { |
| 113 | 1 | if (!is_array($item)) { |
| 114 | 0 | continue; |
| 115 | } | |
| 116 | 1 | $view = (string) ($item['view'] ?? ''); |
| 117 | 1 | $display = (string) ($item['display'] ?? ''); |
| 118 | 1 | if ($view !== $parts[1] || $display !== $parts[2]) { |
| 119 | 0 | continue; |
| 120 | } | |
| 121 | 1 | $keyword = SiteSearchQuery::scalar($item['keyword'] ?? NULL); |
| 122 | 1 | return $keyword === '' ? NULL : $keyword; |
| 123 | } | |
| 124 | 1 | return NULL; |
| 125 | } | |
| 126 | ||
| 127 | } |