5 * Contains search_api_synonym.module.
8 use Drupal\Core\Routing\RouteMatchInterface;
11 * Denotes that the synonym is not active.
13 const SYNONYM_NOT_ACTIVE = 0;
16 * Denotes that the synonym is active.
18 const SYNONYM_ACTIVE = 1;
21 * Implements hook_help().
23 function search_api_synonym_help($route_name, RouteMatchInterface $route_match) {
24 switch ($route_name) {
25 // Main module help for the search_api_synonym module.
26 case 'help.page.search_api_synonym':
28 $output .= '<h3>' . t('About') . '</h3>';
29 $output .= '<p>' . t('Managing of search synonyms in Drupal.') . '</p>';
38 * Implements hook_cron().
40 function search_api_synonym_cron() {
41 $request_time = \Drupal::time()->getRequestTime();
43 // Export synonyms to files.
44 $conf = \Drupal::configFactory()->getEditable('search_api_synonym.settings')->get('cron');
45 $interval = !empty($conf['interval']) ? $conf['interval'] : 86400;
46 $next_execution = \Drupal::state()->get('search_api_synonym.export.next_execution', 0);
48 if ($request_time >= $next_execution) {
49 $logger = \Drupal::logger('search_api_synonym');
51 $logger->notice('Executing export');
54 $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
56 // Validate option: plugin
57 $plugin = $conf['plugin'];
58 if (!$pluginManager->validatePlugin($plugin)) {
59 $logger->warning('Export plugin not found');
63 // Setting non language specific export options
65 'type' => $conf['type'],
66 'filter' => $conf['filter'],
68 'incremental' => $conf['export_if_changed'] ? $next_execution : 0
71 // Get all languages in the system
72 $languages = \Drupal::languageManager()->getLanguages();
74 foreach ($languages as $language) {
75 $options['langcode'] = $language->getId();
77 // Export synonyms with and without spaces into separate files
78 if ($conf['separate_files'] && ($conf['filter'] == 'none' || !$conf['filter'])) {
80 $options['filter'] = 'nospace';
81 search_api_synonym_execute_single_import($plugin, $options);
84 $options['filter'] = 'onlyspace';
85 search_api_synonym_execute_single_import($plugin, $options);
89 search_api_synonym_execute_single_import($plugin, $options);
93 $logger->info('Export done');
95 \Drupal::state()->set('search_api_synonym.export.next_execution', $request_time + $interval);
101 * Execute single export.
103 * @param string $plugin
106 * @param array $options
107 * Array of export options
109 function search_api_synonym_execute_single_import($plugin, $options) {
111 $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
114 $logger = \Drupal::logger('search_api_synonym');
116 $pluginManager->setPluginId($plugin);
117 $pluginManager->setExportOptions($options);
118 if ($result = $pluginManager->executeExport()) {
119 $logger->info('Synonyms export to {filename}', ['filename' => $result]);