search_api_synonym.module in Search API Synonym 8
File
search_api_synonym.module
View source
<?php
use Drupal\Core\Routing\RouteMatchInterface;
const SYNONYM_NOT_ACTIVE = 0;
const SYNONYM_ACTIVE = 1;
function search_api_synonym_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.search_api_synonym':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Managing of search synonyms in Drupal.') . '</p>';
return $output;
default:
return '';
}
}
function search_api_synonym_cron() {
$request_time = \Drupal::time()
->getRequestTime();
$conf = \Drupal::configFactory()
->getEditable('search_api_synonym.settings')
->get('cron');
$interval = isset($conf['interval']) ? $conf['interval'] : 86400;
$next_execution = \Drupal::state()
->get('search_api_synonym.export.next_execution', 0);
if ($interval !== 0 && $request_time >= $next_execution) {
$logger = \Drupal::logger('search_api_synonym');
$logger
->notice('Executing export');
$pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
$plugin = $conf['plugin'];
if (!$pluginManager
->validatePlugin($plugin)) {
$logger
->warning('Export plugin not found');
return;
}
$options = [
'type' => $conf['type'],
'filter' => $conf['filter'],
'file' => '',
'incremental' => $conf['export_if_changed'] ? $next_execution : 0,
];
$languages = \Drupal::languageManager()
->getLanguages();
foreach ($languages as $language) {
$options['langcode'] = $language
->getId();
if ($conf['separate_files'] && ($conf['filter'] == 'none' || !$conf['filter'])) {
$options['filter'] = 'nospace';
search_api_synonym_execute_single_import($plugin, $options);
$options['filter'] = 'onlyspace';
search_api_synonym_execute_single_import($plugin, $options);
}
else {
search_api_synonym_execute_single_import($plugin, $options);
}
}
$logger
->info('Export done');
\Drupal::state()
->set('search_api_synonym.export.next_execution', $request_time + $interval);
}
}
function search_api_synonym_execute_single_import($plugin, $options) {
$pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
$logger = \Drupal::logger('search_api_synonym');
$pluginManager
->setPluginId($plugin);
$pluginManager
->setExportOptions($options);
if ($result = $pluginManager
->executeExport()) {
$logger
->info('Synonyms export to {filename}', [
'filename' => $result,
]);
}
}