You are here

class EntityGetBySynonym in Synonyms 8

Service to look up an entity by its name or synonym.

Hierarchy

Expanded class hierarchy of EntityGetBySynonym

1 string reference to 'EntityGetBySynonym'
synonyms.services.yml in ./synonyms.services.yml
synonyms.services.yml
1 service uses EntityGetBySynonym
synonyms.entity_get_by_synonym in ./synonyms.services.yml
Drupal\synonyms\SynonymsService\EntityGetBySynonym

File

src/SynonymsService/EntityGetBySynonym.php, line 13

Namespace

Drupal\synonyms\SynonymsService
View source
class EntityGetBySynonym {

  /**
   * The find synonyms service.
   *
   * @var \Drupal\synonyms\SynonymsService\FindSynonyms
   */
  protected $findSynonymsService;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * EntityGetBySynonym constructor.
   */
  public function __construct(FindSynonyms $find_synonyms_service, EntityTypeManagerInterface $entity_type_manager) {
    $this->findSynonymsService = $find_synonyms_service;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Try finding entities by their name or synonym.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   What entity type is being searched.
   * @param string $name
   *   The look up keyword (the supposed name or synonym).
   * @param string $bundle
   *   Optionally limit the search within a specific bundle name of the provided
   *   entity type.
   *
   * @return array
   *   IDs of the looked up entities. If such entity is not found,
   *   an empty array is returned.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityGetBySynonym::$entityTypeManager protected property The entity type manager.
EntityGetBySynonym::$findSynonymsService protected property The find synonyms service.
EntityGetBySynonym::entityGetBySynonym public function Try finding entities by their name or synonym.
EntityGetBySynonym::__construct public function EntityGetBySynonym constructor.