You are here

public static function EntityHelper::queryIdsByLabel in Helper 7

Fetch entity IDs that have a certain label.

Parameters

string $entity_type: The entity type of $entity.

string $label: The label of the entity to load.

string $bundle: An optional bundle to restrict the results to.

object $query: An optional EntityFieldQuery object to use to perform the query.

Return value

array An array of matching entity IDs.

Throws

Exception

1 call to EntityHelper::queryIdsByLabel()
EntityHelper::queryIdByLabel in lib/EntityHelper.php
Fetch an entity ID that have a certain label.

File

lib/EntityHelper.php, line 69

Class

EntityHelper

Code

public static function queryIdsByLabel($entity_type, $label, $bundle = NULL, $query = NULL) {
  $info = entity_get_info($entity_type);
  if (empty($info['entity keys']['label'])) {
    throw new Exception("Unable to load entities of type {$entity_type} by label.");
  }
  if (!isset($query)) {
    $query = new EntityFieldQuery();
    $query
      ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
  }
  $query
    ->entityCondition('entity_type', $entity_type);
  if (isset($bundle)) {
    $query
      ->entityCondition('bundle', $bundle);
  }
  $query
    ->propertyCondition($info['entity keys']['label'], $label);
  $results = $query
    ->execute();
  if (!empty($results[$entity_type])) {
    return array_keys($results[$entity_type]);
  }
  else {
    return array();
  }
}