You are here

synonyms_search.module in Synonyms 8

Same filename and directory in other branches
  1. 7 synonyms_search/synonyms_search.module

Integrates Synonyms with core Search module.

File

synonyms_search/synonyms_search.module
View source
<?php

/**
 * @file
 * Integrates Synonyms with core Search module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\synonyms\SynonymInterface;

/**
 * Implements hook_entity_view().
 */
function synonyms_search_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  \Drupal::service('synonyms.behavior.search')
    ->entityView($build, $entity, $display, $view_mode);
}

/**
 * Implements hook_entity_update().
 */
function synonyms_search_entity_update(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    \Drupal::service('synonyms.behavior.search')
      ->entityMarkForReindex($entity);
  }
}

/**
 * Implements hook_entity_delete().
 */
function synonyms_search_entity_delete(EntityInterface $entity) {
  if ($entity instanceof ContentEntityInterface) {
    \Drupal::service('synonyms.behavior.search')
      ->entityMarkForReindex($entity);
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function synonyms_search_synonym_insert(EntityInterface $entity) {
  synonyms_search_synonym_reindex($entity);
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function synonyms_search_synonym_update(EntityInterface $entity) {
  synonyms_search_synonym_reindex($entity);
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function synonyms_search_synonym_delete(EntityInterface $entity) {
  synonyms_search_synonym_reindex($entity);
}

/**
 * Mark all search index dependent on a given synonym config for reindexing.
 *
 * @param \Drupal\synonyms\SynonymInterface $synonym
 *   Synonym config whose dependent search index should be marked
 *   for reindexing.
 */
function synonyms_search_synonym_reindex(SynonymInterface $synonym) {
  if ($synonym
    ->get('behavior') == 'synonyms.behavior.search') {
    $entity_type = \Drupal::entityTypeManager()
      ->getDefinition($synonym
      ->getProviderPluginInstance()
      ->getPluginDefinition()['controlled_entity_type']);
    $bundle = $synonym
      ->getProviderPluginInstance()
      ->getPluginDefinition()['controlled_bundle'];
    $query = \Drupal::entityQuery($entity_type
      ->id());
    $query
      ->condition($entity_type
      ->getKey('bundle'), $bundle);
    $result = $query
      ->execute();
    \Drupal::service('synonyms.behavior.search')
      ->entityMarkForReindexMultiple(array_values($result), $entity_type
      ->id());
  }
}