ProviderService.php in Synonyms 2.0.x
File
src/SynonymsService/ProviderService.php
View source
<?php
namespace Drupal\synonyms\SynonymsService;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\synonyms\ProviderInterface\FindInterface;
class ProviderService {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function getEntitySynonyms(ContentEntityInterface $entity) {
$synonyms = [];
foreach ($this
->getSynonymConfigEntities($entity
->getEntityTypeId(), $entity
->bundle()) as $synonym_config) {
$synonyms = array_merge($synonyms, $synonym_config
->getProviderPluginInstance()
->getSynonyms($entity));
}
return array_unique($synonyms);
}
public function getSynonymConfigEntities($entity_type, $bundle) {
$entities = [];
if (is_scalar($bundle) && !is_null($bundle)) {
$bundle = [
$bundle,
];
}
foreach ($this->entityTypeManager
->getStorage('synonym')
->loadMultiple() as $synonym_config) {
$provider_instance = $synonym_config
->getProviderPluginInstance();
$provider_definition = $provider_instance
->getPluginDefinition();
if ($provider_definition['controlled_entity_type'] == $entity_type && (!is_array($bundle) || in_array($provider_definition['controlled_bundle'], $bundle))) {
$entities[] = $synonym_config;
}
}
return $entities;
}
public function findSynonyms(Condition $condition, EntityTypeInterface $entity_type, $bundle = NULL) {
if (!$entity_type
->getKey('bundle')) {
$bundle = $entity_type
->id();
}
$lookup = [];
if (is_null($bundle)) {
$bundle = array_keys($this->entityTypeBundleInfo
->getBundleInfo($entity_type
->id()));
}
foreach ($this
->getSynonymConfigEntities($entity_type
->id(), $bundle) as $synonym_config) {
foreach ($synonym_config
->getProviderPluginInstance()
->synonymsFind(clone $condition) as $synonym) {
$lookup[] = $synonym;
}
}
return $lookup;
}
public function getBySynonym(EntityTypeInterface $entity_type, $name, $bundle = NULL) {
$found_entity_ids = [];
if ($entity_type
->id() == 'user' || $entity_type
->hasKey('label')) {
$label_column = $entity_type
->id() == 'user' ? 'name' : $entity_type
->getKey('label');
$query = $this->entityTypeManager
->getStorage($entity_type
->id())
->getQuery();
$query
->condition($label_column, $name);
if ($entity_type
->hasKey('bundle') && $bundle) {
$query
->condition($entity_type
->getKey('bundle'), $bundle);
}
if ($result = $query
->execute()) {
foreach ($result as $id) {
$found_entity_ids[] = $id;
}
}
}
$condition = new Condition('AND');
$condition
->condition(FindInterface::COLUMN_SYNONYM_PLACEHOLDER, $name);
foreach ($this
->findSynonyms($condition, $entity_type, $bundle) as $item) {
$found_entity_ids[] = $item->entity_id;
}
return array_unique($found_entity_ids);
}
public function serviceIsEnabled($entity_type, $bundle, $service_id) {
$status = \Drupal::config('synonyms_' . $service_id . '.behavior.' . $entity_type . '.' . $bundle)
->get('status');
return !empty($status);
}
}