public function ProviderService::getBySynonym in Synonyms 2.0.x
Try finding entities by their name or synonym.
Parameters
\Drupal\Core\Entity\EntityTypeInterface $entity_type: What entity type is being searched.
string $name: The look up keyword (the supposed name or synonym).
string $bundle: Optionally limit the search within a specific bundle name of the provided entity type.
Return value
array IDs of the looked up entities. If such entity is not found, an empty array is returned.
File
- src/
SynonymsService/ ProviderService.php, line 151
Class
- ProviderService
- A collection of handy provider-related methods.
Namespace
Drupal\synonyms\SynonymsServiceCode
public function getBySynonym(EntityTypeInterface $entity_type, $name, $bundle = NULL) {
$found_entity_ids = [];
if ($entity_type
->id() == 'user' || $entity_type
->hasKey('label')) {
// User entity type does not declare its label, while it does have one.
$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);
}