Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / search_api_synonym.module
1 <?php
2
3 /**
4  * @file
5  * Contains search_api_synonym.module.
6  */
7
8 use Drupal\Core\Routing\RouteMatchInterface;
9
10 /**
11  * Denotes that the synonym is not active.
12  */
13 const SYNONYM_NOT_ACTIVE = 0;
14
15 /**
16  * Denotes that the synonym is active.
17  */
18 const SYNONYM_ACTIVE = 1;
19
20 /**
21  * Implements hook_help().
22  */
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':
27       $output = '';
28       $output .= '<h3>' . t('About') . '</h3>';
29       $output .= '<p>' . t('Managing of search synonyms in Drupal.') . '</p>';
30       return $output;
31
32     default:
33       return '';
34   }
35 }
36
37 /**
38  * Implements hook_cron().
39  */
40 function search_api_synonym_cron() {
41   $request_time = \Drupal::time()->getRequestTime();
42
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);
47
48   if ($request_time >= $next_execution) {
49     $logger = \Drupal::logger('search_api_synonym');
50     // Execute export
51     $logger->notice('Executing export');
52
53     // Plugin manager
54     $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
55
56     // Validate option: plugin
57     $plugin = $conf['plugin'];
58     if (!$pluginManager->validatePlugin($plugin)) {
59       $logger->warning('Export plugin not found');
60       return;
61     }
62
63     // Setting non language specific export options
64     $options = [
65       'type' => $conf['type'],
66       'filter' => $conf['filter'],
67       'file' => '',
68       'incremental' => $conf['export_if_changed'] ? $next_execution : 0
69     ];
70
71     // Get all languages in the system
72     $languages = \Drupal::languageManager()->getLanguages();
73
74     foreach ($languages as $language) {
75       $options['langcode'] = $language->getId();
76
77       // Export synonyms with and without spaces into separate files
78       if ($conf['separate_files'] && ($conf['filter'] == 'none' || !$conf['filter'])) {
79         // Without spaces
80         $options['filter'] = 'nospace';
81         search_api_synonym_execute_single_import($plugin, $options);
82
83         // With spaces
84         $options['filter'] = 'onlyspace';
85         search_api_synonym_execute_single_import($plugin, $options);
86
87       }
88       else {
89         search_api_synonym_execute_single_import($plugin, $options);
90       }
91     }
92
93     $logger->info('Export done');
94
95     \Drupal::state()->set('search_api_synonym.export.next_execution', $request_time + $interval);
96   }
97
98 }
99
100 /**
101  * Execute single export.
102  *
103  * @param string $plugin
104  *   Plugin name
105  *
106  * @param array $options
107  *   Array of export options
108  */
109 function search_api_synonym_execute_single_import($plugin, $options) {
110   // Plugin manager
111   $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
112
113   // Logger
114   $logger = \Drupal::logger('search_api_synonym');
115
116   $pluginManager->setPluginId($plugin);
117   $pluginManager->setExportOptions($options);
118   if ($result = $pluginManager->executeExport()) {
119     $logger->info('Synonyms export to {filename}', ['filename' => $result]);
120   }
121
122 }