You are here

function synonyms_get_entity_by_synonym in Synonyms 7

Try finding an entity by its name or synonym.

Parameters

string $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

int ID of the looked up entity. If such entity was not found, then 0 is returned

3 calls to synonyms_get_entity_by_synonym()
SynonymsSynonymsWebTestCase::testSynonyms in ./synonyms.test
Test the functionality of synonyms.
synonyms_entity_find_feeds_tamper_callback in plugins/feeds_tamper/synonyms_entity_find.inc
Feeds tamper callback to execute entity look up by its synonyms.
synonyms_views_handler_filter_entityreference_synonyms::query in views/synonyms_views_handler_filter_entityreference_synonyms.inc
Add this filter to the query.

File

./synonyms.module, line 541
Provide synonyms feature for Drupal entities.

Code

function synonyms_get_entity_by_synonym($entity_type, $name, $bundle = NULL) {
  $name = trim($name);
  $entity_info = entity_get_info($entity_type);

  // This is somewhat hacky, but it's the best we can do: user.module does not
  // declare 'label' entity key on 'user' entity type, while there is clearly
  // one: the 'name' column. In fact, entityreference.module does about the same
  // thing in EntityReference_SelectionHandler_Generic_user class.
  if ($entity_type == 'user') {
    $entity_info['entity keys']['label'] = 'name';
  }
  if (isset($entity_info['entity keys']['label'])) {
    $efq = new EntityFieldQuery();
    $efq
      ->entityCondition('entity_type', $entity_type);
    if ($bundle) {
      $efq
        ->entityCondition('bundle', $bundle);
    }
    $efq
      ->propertyCondition($entity_info['entity keys']['label'], $name);
    $result = $efq
      ->execute();
    if (isset($result[$entity_type])) {
      $result = array_keys($result[$entity_type]);
      return reset($result);
    }
  }
  $synonyms = synonyms_synonyms_find(db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $name), $entity_type, $bundle);
  if (!empty($synonyms)) {
    return reset($synonyms)->entity_id;
  }
  return 0;
}