You are here

protected function EntityUsageLastEntityQueryTrait::getLastEntityOfType in Entity Usage 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Traits/EntityUsageLastEntityQueryTrait.php \Drupal\Tests\entity_usage\Traits\EntityUsageLastEntityQueryTrait::getLastEntityOfType()

Gets the latest entity created of a given type.

Will fail the test if there is no entity of that type.

Parameters

string $entity_type_id: The storage name of the entity.

bool $load: (optional) Whether or not the return thould be the loaded entity. Defaults to FALSE.

Return value

mixed The ID of the latest created entity of that type. If $load is TRUE, will use ::loadUnchanged() to get a fresh version of the entity object and return it.

13 calls to EntityUsageLastEntityQueryTrait::getLastEntityOfType()
ConfigEntityTrackingTest::testBlockFieldCustomBlocksTracking in tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php
Tests block_field / custom_blocks tracking.
ConfigEntityTrackingTest::testBlockFieldViewsTracking in tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php
Tests block_field / views tracking.
DynamicEntityReferenceTest::testDynamicEntityReferenceTracking in tests/src/FunctionalJavascript/DynamicEntityReferenceTest.php
Tests the tracking of entities through dynamic entity reference fields.
EmbeddedContentTest::testEntityEmbed in tests/src/FunctionalJavascript/EmbeddedContentTest.php
Tests the Entity Embed parsing.
EmbeddedContentTest::testHtmlLink in tests/src/FunctionalJavascript/EmbeddedContentTest.php
Tests the HtmlLink parsing.

... See full list

File

tests/src/Traits/EntityUsageLastEntityQueryTrait.php, line 26

Class

EntityUsageLastEntityQueryTrait
Test trait providing helpers to query latest entities created.

Namespace

Drupal\Tests\entity_usage\Traits

Code

protected function getLastEntityOfType($entity_type_id, $load = FALSE) {
  $query_result = \Drupal::entityQuery($entity_type_id)
    ->sort('created', 'DESC')
    ->range(0, 1)
    ->execute();
  $entity_id = reset($query_result);
  if (empty($entity_id)) {
    $this
      ->fail('Could not find latest entity of type: ' . $entity_type_id);
  }
  if ($load) {
    return \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->loadUnchanged($entity_id);
  }
  else {
    return $entity_id;
  }
}