EntityGetBySynonym.php in Synonyms 8
File
src/SynonymsService/EntityGetBySynonym.php
View source
<?php
namespace Drupal\synonyms\SynonymsService;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\synonyms\SynonymsProviderInterface\SynonymsFindProviderInterface;
class EntityGetBySynonym {
protected $findSynonymsService;
protected $entityTypeManager;
public function __construct(FindSynonyms $find_synonyms_service, EntityTypeManagerInterface $entity_type_manager) {
$this->findSynonymsService = $find_synonyms_service;
$this->entityTypeManager = $entity_type_manager;
}
public function entityGetBySynonym(EntityTypeInterface $entity_type, $name, $bundle = NULL) {
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);
}
$result = $query
->execute();
$result = reset($result);
if ($result) {
return $result;
}
}
$condition = new Condition('AND');
$condition
->condition(SynonymsFindProviderInterface::COLUMN_SYNONYM_PLACEHOLDER, $name);
$found_entity_ids = [];
$synonyms_found = $this->findSynonymsService
->findSynonyms($condition, $entity_type, $bundle, '*');
if (isset($synonyms_found[0]->entity_id)) {
$found_entity_ids[] = $synonyms_found[0]->entity_id;
}
return $found_entity_ids;
}
}