You are here

search_api_synonym.module in Search API Synonym 8

File

search_api_synonym.module
View source
<?php

/**
 * @file
 * Contains search_api_synonym.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Denotes that the synonym is not active.
 */
const SYNONYM_NOT_ACTIVE = 0;

/**
 * Denotes that the synonym is active.
 */
const SYNONYM_ACTIVE = 1;

/**
 * Implements hook_help().
 */
function search_api_synonym_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the search_api_synonym module.
    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 '';
  }
}

/**
 * Implements hook_cron().
 */
function search_api_synonym_cron() {
  $request_time = \Drupal::time()
    ->getRequestTime();

  // Export synonyms to files.
  $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');

    // Execute export
    $logger
      ->notice('Executing export');

    // Plugin manager
    $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');

    // Validate option: plugin
    $plugin = $conf['plugin'];
    if (!$pluginManager
      ->validatePlugin($plugin)) {
      $logger
        ->warning('Export plugin not found');
      return;
    }

    // Setting non language specific export options
    $options = [
      'type' => $conf['type'],
      'filter' => $conf['filter'],
      'file' => '',
      'incremental' => $conf['export_if_changed'] ? $next_execution : 0,
    ];

    // Get all languages in the system
    $languages = \Drupal::languageManager()
      ->getLanguages();
    foreach ($languages as $language) {
      $options['langcode'] = $language
        ->getId();

      // Export synonyms with and without spaces into separate files
      if ($conf['separate_files'] && ($conf['filter'] == 'none' || !$conf['filter'])) {

        // Without spaces
        $options['filter'] = 'nospace';
        search_api_synonym_execute_single_import($plugin, $options);

        // With spaces
        $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);
  }
}

/**
 * Execute single export.
 *
 * @param string $plugin
 *   Plugin name
 *
 * @param array $options
 *   Array of export options
 */
function search_api_synonym_execute_single_import($plugin, $options) {

  // Plugin manager
  $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');

  // Logger
  $logger = \Drupal::logger('search_api_synonym');
  $pluginManager
    ->setPluginId($plugin);
  $pluginManager
    ->setExportOptions($options);
  if ($result = $pluginManager
    ->executeExport()) {
    $logger
      ->info('Synonyms export to {filename}', [
      'filename' => $result,
    ]);
  }
}

Functions

Constants

Namesort descending Description
SYNONYM_ACTIVE Denotes that the synonym is active.
SYNONYM_NOT_ACTIVE Denotes that the synonym is not active.