You are here

protected function ImageFieldTokensTestingTrait::entityGetDisplay in ImageField Tokens 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ImageFieldTokensTestingTrait.php \Drupal\Tests\imagefield_tokens\Functional\ImageFieldTokensTestingTrait::entityGetDisplay()

Returns the entity view display associated with a bundle and view mode.

Use this function when assigning suggested display options for a component in a given view mode. Note that they will only be actually used at render time if the view mode itself is configured to use dedicated display settings for the bundle; if not, the 'default' display is used instead.

Parameters

string $entity_type: The entity type.

string $bundle: The bundle.

string $view_mode: The view mode, or 'default' to retrieve the 'default' display object for this bundle.

Return value

\Drupal\Core\Entity\Display\EntityViewDisplayInterface The entity view display associated with the view mode.

See also

\Drupal\Core\Entity\EntityStorageInterface::create()

\Drupal\Core\Entity\EntityStorageInterface::load()

File

tests/src/Functional/ImageFieldTokensTestingTrait.php, line 102

Class

ImageFieldTokensTestingTrait
Provides a helper method for creating Image fields.

Namespace

Drupal\Tests\imagefield_tokens\Functional

Code

protected function entityGetDisplay($entity_type, $bundle, $view_mode) : EntityViewDisplayInterface {

  // Try loading the display from configuration.
  $display = EntityViewDisplay::load($entity_type . '.' . $bundle . '.' . $view_mode);

  // If not found, create a fresh display object. We do not primitively
  // create new entity_view_display configuration entries for each existing
  // entity type and bundle whenever a new view mode becomes available.
  // Instead, configuration entries are only created when a display object is
  // explicitly configured and saved.
  if (!$display) {
    $display = EntityViewDisplay::create([
      'targetEntityType' => $entity_type,
      'bundle' => $bundle,
      'mode' => $view_mode,
      'status' => TRUE,
    ]);
  }
  return $display;
}