coveo-1.0.0-alpha1/modules/coveo_atomic/coveo_atomic.module
modules/coveo_atomic/coveo_atomic.module
<?php
/**
* @file
* Provides module features for Coveo's Atomic search interface.
*/
declare(strict_types=1);
use Drupal\Core\Hook\Attribute\LegacyHook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\coveo\Entity\CoveoSearchComponent;
use Drupal\coveo_atomic\Hook\CoveoAtomicHooks;
/**
* Implements hook_help().
*/
#[LegacyHook]
function coveo_atomic_help(string $route_name, RouteMatchInterface $route_match) {
return \Drupal::service(CoveoAtomicHooks::class)->help($route_name, $route_match);
}
/**
* Implements hook_theme().
*/
#[LegacyHook]
function coveo_atomic_theme(): array {
return \Drupal::service(CoveoAtomicHooks::class)->theme();
}
/**
* Prepares variables for atomic search element templates.
*
* Default template: coveo-atomic-search.html.twig.
*
* Mostly this does the magic of attaching settings.
*
* @param array $variables
* An associative array containing:
* - id: An associative array containing the properties of the element.
* Properties used: #title, #value, #options, #description, #extra,
* #multiple, #required, #name, #attributes, #size, #sort_options,
* #sort_start.
*/
function template_preprocess_coveo_atomic_search(array &$variables) {
if (!empty($variables['search'])) {
$search = CoveoSearchComponent::load($variables['search']);
$variables['#attached'] = [
'library' => ['coveo_atomic/atomic'],
'drupalSettings' => [
'coveo_atomic' => [
$variables['id'] => [
'search' => $variables['search'],
'accessToken' => $search?->getToken(
\Drupal::request(),
\Drupal::currentUser(),
),
'organizationId' => $search?->getOrganizationId(),
'analyticsMode' => 'legacy',
],
],
],
];
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
#[LegacyHook]
function coveo_atomic_theme_suggestions_coveo_atomic_search(array $variables): array {
return \Drupal::service(CoveoAtomicHooks::class)->themeSuggestionsCoveoAtomicSearch($variables);
}
