View source
<?php
namespace Drupal\synonyms_search\SynonymsService\Behavior;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\synonyms\Entity\Synonym;
use Drupal\synonyms\SynonymsService\Behavior\SynonymsBehaviorInterface;
use Drupal\synonyms\SynonymsService\BehaviorService;
class SearchService implements SynonymsBehaviorInterface {
use StringTranslationTrait;
const BEHAVIOR = 'synonyms.behavior.search';
protected $entityFieldManager;
protected $entityTypeManager;
protected $behaviorService;
protected $database;
protected $time;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, BehaviorService $behavior_service, Connection $database, TimeInterface $time) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->behaviorService = $behavior_service;
$this->database = $database;
$this->time = $time;
}
public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($entity instanceof ContentEntityInterface && $view_mode == 'search_index') {
$synonyms = [];
$cacheable_metadata = new CacheableMetadata();
$entity_references = array_filter($this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle()), function ($item) {
if ($item
->getType() == 'entity_reference') {
$target_entity_type = $this->entityTypeManager
->getDefinition($item
->getSetting('target_type'));
return $target_entity_type instanceof ContentEntityTypeInterface;
}
return FALSE;
});
foreach ($entity_references as $entity_reference) {
foreach ($entity
->get($entity_reference
->getName())
->referencedEntities() as $target_entity) {
$synonyms = array_merge($synonyms, $this
->getEntitySynonyms($target_entity));
$cacheable_metadata
->addCacheableDependency($target_entity);
$cacheable_metadata
->addCacheTags([
Synonym::cacheTagConstruct(self::BEHAVIOR, $target_entity
->getEntityTypeId(), $target_entity
->bundle()),
]);
}
}
$build['synonyms_search'] = [
'#markup' => implode(', ', $synonyms),
];
$cacheable_metadata
->applyTo($build['synonyms_search']);
}
}
public function entityMarkForReindex(ContentEntityInterface $entity) {
$this
->entityMarkForReindexMultiple([
$entity
->id(),
], $entity
->getEntityTypeId());
}
public function entityMarkForReindexMultiple(array $entity_ids, $entity_type) {
if (empty($entity_ids)) {
return;
}
$map = $this->entityFieldManager
->getFieldMapByFieldType('entity_reference');
foreach ($map as $host_entity_type => $fields) {
foreach ($fields as $field_name => $field_info) {
$field = FieldStorageConfig::loadByName($host_entity_type, $field_name);
if ($field && $field
->getSetting('target_type') == $entity_type) {
$query = $this->entityTypeManager
->getStorage($host_entity_type)
->getQuery();
$query
->condition($field_name, $entity_ids, 'IN');
$result = $query
->execute();
if (!empty($result)) {
$this->database
->update('search_dataset')
->fields([
'reindex' => $this->time
->getRequestTime(),
])
->condition('reindex', 0)
->condition('type', $host_entity_type . '_search')
->condition('sid', array_values($result), 'IN')
->execute();
}
}
}
}
}
public function getTitle() {
return $this
->t('Search');
}
protected function getEntitySynonyms(ContentEntityInterface $entity) {
$synonyms = [];
foreach ($this->behaviorService
->getSynonymConfigEntities(self::BEHAVIOR, $entity
->getEntityTypeId(), $entity
->bundle()) as $synonym_config) {
$synonyms = array_merge($synonyms, $synonym_config
->getProviderPluginInstance()
->getSynonyms($entity));
}
return $synonyms;
}
}