Coverage index

src/Plugin/VisitorsSiteSearch/ViewDisplay.php

Lines: 89.19%

LineHitsSource
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
  ) {
5011
    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 {
5711
    return new static(
5811
      $configuration,
5911
      $plugin_id,
6011
      $plugin_definition,
6111
      $container->get('request_stack'),
6211
      $container->get('config.factory'),
6311
    );
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public function applies(string $route, string $path, array $query): bool {
7010
    return $this->keywordParameter($route) !== NULL;
71
  }
72
73
  /**
74
   * {@inheritdoc}
75
   */
76
  public function parse(string $route, string $path, array $query): ?SiteSearchHit {
771
    $parameter = $this->keywordParameter($route);
781
    if ($parameter === NULL) {
790
      return NULL;
80
    }
811
    $keyword = SiteSearchQuery::scalar($query[$parameter] ?? NULL);
821
    if ($keyword === '') {
831
      return NULL;
84
    }
85
861
    return new SiteSearchHit(
871
      $this->getPluginId(),
881
      $keyword,
891
      SiteSearchQuery::page($query),
901
      SiteSearchQuery::canonical($query, [$parameter]),
911
    );
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 {
10410
    $parts = explode('.', $route, 3);
10510
    if (count($parts) !== 3 || $parts[0] !== 'view' || $parts[1] === '' || $parts[2] === '') {
1069
      return NULL;
107
    }
1081
    $items = $this->configFactory->get('visitors.settings')->get('site_search_views');
1091
    if (!is_array($items)) {
1100
      return NULL;
111
    }
1121
    foreach ($items as $item) {
1131
      if (!is_array($item)) {
1140
        continue;
115
      }
1161
      $view = (string) ($item['view'] ?? '');
1171
      $display = (string) ($item['display'] ?? '');
1181
      if ($view !== $parts[1] || $display !== $parts[2]) {
1190
        continue;
120
      }
1211
      $keyword = SiteSearchQuery::scalar($item['keyword'] ?? NULL);
1221
      return $keyword === '' ? NULL : $keyword;
123
    }
1241
    return NULL;
125
  }
126
127
}