You are here

protected function OgMenuSubContext::getEntityByLabel in Organic Groups Menu (OG Menu) 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

\Exception Thrown when an entity with the given type, label and bundle does not exist.

1 call to OgMenuSubContext::getEntityByLabel()
OgMenuSubContext::assertMenuItemCount in ./og_menu.behat.inc
Checks that the given OG Menu instance has the expected number of items.

File

./og_menu.behat.inc, line 60
Contains \OgMenuSubContext.

Class

OgMenuSubContext
Behat step definitions for testing OG Menus.

Code

protected function getEntityByLabel($entity_type, $label, $bundle = NULL) {
  $entity_manager = \Drupal::entityTypeManager();
  $storage = $entity_manager
    ->getStorage($entity_type);
  $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 \Exception("The entity with label '{$label}' was not found.");
}