protected function MessageDigestSubContext::getEntityByLabel in Message Digest 8
Returns the entity with the given type, bundle and label.
If multiple entities have the same label then the first one is returned.
Parameters
string $entity_type: The entity type to check.
string $label: The label to check.
string $bundle: Optional bundle to check. If omitted, the entity can be of any bundle.
Return value
\Drupal\Core\Entity\EntityInterface The requested entity.
Throws
\RuntimeException Thrown when an entity with the given type, label and bundle does not exist.
File
- ./
message_digest.behat.inc, line 178 - Contains \MessageDigestSubContext.
Class
- MessageDigestSubContext
- Behat step definitions for the Message Digest module.
Code
protected function getEntityByLabel($entity_type, $label, $bundle = NULL) {
$entity_manager = \Drupal::entityTypeManager();
try {
$storage = $entity_manager
->getStorage($entity_type);
} catch (InvalidPluginDefinitionException $e) {
throw new \RuntimeException("Storage for entity type '{$entity_type}' not found", NULL, $e);
}
$entity = $entity_manager
->getDefinition($entity_type);
$query = $storage
->getQuery()
->condition($entity
->getKey('label'), $label)
->range(0, 1);
// Optionally filter by bundle.
if ($bundle) {
$query
->condition($entity
->getKey('bundle'), $bundle);
}
$result = $query
->execute();
if ($result) {
$result = reset($result);
return $storage
->load($result);
}
throw new \RuntimeException("The entity with label '{$label}' was not found.");
}