Coverage index

src/Plugin/VisitorsSiteSearch/SearchApi.php

Lines: 84.75%

LineHitsSource
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
  ) {
5111
    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 {
5811
    return new static(
5911
      $configuration,
6011
      $plugin_id,
6111
      $plugin_definition,
6211
      $container->get('request_stack'),
6311
      $container->get('entity_type.manager'),
6411
    );
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function applies(string $route, string $path, array $query): bool {
7111
    return $this->keywordParameter($route) !== NULL;
72
  }
73
74
  /**
75
   * {@inheritdoc}
76
   */
77
  public function parse(string $route, string $path, array $query): ?SiteSearchHit {
781
    $parameter = $this->keywordParameter($route);
791
    if ($parameter === NULL) {
800
      return NULL;
81
    }
821
    $keyword = SiteSearchQuery::scalar($query[$parameter] ?? NULL);
831
    if ($keyword === '') {
840
      return NULL;
85
    }
86
871
    return new SiteSearchHit(
881
      $this->getPluginId(),
891
      $keyword,
901
      SiteSearchQuery::page($query),
911
      SiteSearchQuery::canonical($query, [$parameter]),
921
    );
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 {
10511
    $ids = $this->viewIds($route);
10611
    if ($ids === NULL) {
1079
      return NULL;
108
    }
1092
    [$view_id, $display_id] = $ids;
1102
    $view = $this->entityTypeManager->getStorage('view')->load($view_id);
1112
    if (!$view instanceof View) {
1120
      return NULL;
113
    }
1142
    $base = (string) $view->get('base_table');
1152
    if (!str_starts_with($base, 'search_api_index_')) {
1161
      return NULL;
117
    }
1181
    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 {
13111
    $parts = explode('.', $route, 3);
13211
    if (count($parts) !== 3 || $parts[0] !== 'view' || $parts[1] === '' || $parts[2] === '') {
1339
      return NULL;
134
    }
1352
    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 {
1501
    $display = $view->getDisplay($display_id);
1511
    if ($display === NULL) {
1520
      return NULL;
153
    }
1541
    $options = $display['display_options'] ?? [];
1551
    $uses_default = ($options['defaults']['filters'] ?? TRUE) !== FALSE;
1561
    if ($uses_default || !isset($options['filters'])) {
1571
      $default = $view->getDisplay('default');
1581
      $options = $default['display_options'] ?? [];
159
    }
1601
    $filters = $options['filters'] ?? [];
1611
    if (!is_array($filters)) {
1620
      return NULL;
163
    }
1641
    foreach ($filters as $key => $filter) {
1651
      if (!is_array($filter)) {
1660
        continue;
167
      }
1681
      $plugin = (string) ($filter['plugin_id'] ?? $filter['id'] ?? '');
1691
      if ($plugin !== 'search_api_fulltext') {
1700
        continue;
171
      }
1721
      $exposed = !empty($filter['exposed']) || !empty($filter['expose']['exposed']);
1731
      if (!$exposed) {
1740
        continue;
175
      }
1761
      $identifier = SiteSearchQuery::scalar($filter['expose']['identifier'] ?? $key);
1771
      return $identifier === '' ? NULL : $identifier;
178
    }
1790
    return NULL;
180
  }
181
182
}