synonyms_search.module 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * Integrates Synonyms with core Search module.
  5. */
  6. /**
  7. * Implements hook_entity_view().
  8. */
  9. function synonyms_search_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  10. \Drupal::service('synonyms.behavior.search')->entityView($build, $entity, $display, $view_mode);
  11. }
  12. /**
  13. * Implements hook_entity_update().
  14. */
  15. function synonyms_search_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  16. if ($entity instanceof \Drupal\Core\Entity\ContentEntityInterface) {
  17. \Drupal::service('synonyms.behavior.search')->entityMarkForReindex($entity);
  18. }
  19. }
  20. /**
  21. * Implements hook_entity_delete().
  22. */
  23. function synonyms_search_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  24. if ($entity instanceof \Drupal\Core\Entity\ContentEntityInterface) {
  25. \Drupal::service('synonyms.behavior.search')->entityMarkForReindex($entity);
  26. }
  27. }
  28. /**
  29. * Implements hook_ENTITY_TYPE_insert().
  30. */
  31. function synonyms_search_synonym_insert(Drupal\Core\Entity\EntityInterface $entity) {
  32. synonyms_search_synonym_reindex($entity);
  33. }
  34. /**
  35. * Implements hook_ENTITY_TYPE_update().
  36. */
  37. function synonyms_search_synonym_update(Drupal\Core\Entity\EntityInterface $entity) {
  38. synonyms_search_synonym_reindex($entity);
  39. }
  40. /**
  41. * Implements hook_ENTITY_TYPE_delete().
  42. */
  43. function synonyms_search_synonym_delete(Drupal\Core\Entity\EntityInterface $entity) {
  44. synonyms_search_synonym_reindex($entity);
  45. }
  46. /**
  47. * Mark all search index dependent on a given synonym config for reindexing.
  48. *
  49. * @param \Drupal\synonyms\SynonymInterface $synonym
  50. * Synonym config whose dependent search index should be marked for reindexing
  51. */
  52. function synonyms_search_synonym_reindex(\Drupal\synonyms\SynonymInterface $synonym) {
  53. if ($synonym->get('behavior') == 'synonyms.behavior.search') {
  54. $entity_type = \Drupal::entityTypeManager()->getDefinition($synonym->getProviderPluginInstance()->getPluginDefinition()['controlled_entity_type']);
  55. $bundle = $synonym->getProviderPluginInstance()->getPluginDefinition()['controlled_bundle'];
  56. $query = \Drupal::entityQuery($entity_type->id());
  57. $query->condition($entity_type->getKey('bundle'), $bundle);
  58. $result = $query->execute();
  59. \Drupal::service('synonyms.behavior.search')->entityMarkForReindexMultiple(array_values($result), $entity_type->id());
  60. }
  61. }