You are here

protected function WorkspaceTestUtilities::getOneEntityByLabel in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php \Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities::getOneEntityByLabel()
  2. 9 core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php \Drupal\Tests\workspaces\Functional\WorkspaceTestUtilities::getOneEntityByLabel()

Loads a single entity by its label.

The UI approach to creating an entity doesn't make it easy to know what the ID is, so this lets us make paths for an entity after it's created.

Parameters

string $type: The type of entity to load.

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

Return value

\Drupal\Core\Entity\EntityInterface The entity.

File

core/modules/workspaces/tests/src/Functional/WorkspaceTestUtilities.php, line 34

Class

WorkspaceTestUtilities
Utility methods for use in BrowserTestBase tests.

Namespace

Drupal\Tests\workspaces\Functional

Code

protected function getOneEntityByLabel($type, $label) {

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $property = $entity_type_manager
    ->getDefinition($type)
    ->getKey('label');
  $entity_list = $entity_type_manager
    ->getStorage($type)
    ->loadByProperties([
    $property => $label,
  ]);
  $entity = current($entity_list);
  if (!$entity) {
    $this
      ->fail("No {$type} entity named {$label} found.");
  }
  return $entity;
}