You are here

function entity_get_display in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/entity.inc \entity_get_display()

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.

The function reads the entity view display from the current configuration, or returns a ready-to-use empty one if configuration entry exists yet for this bundle and view mode. This streamlines manipulation of display objects by always returning a consistent object that reflects the current state of the configuration.

Example usage:

  • Set the 'body' field to be displayed and the 'field_image' field to be hidden on article nodes in the 'default' display.

entity_get_display('node', 'article', 'default')
  ->setComponent('body', array(
    'type' => 'text_summary_or_trimmed',
    'settings' => array('trim_length' => '200')
    'weight' => 1,
  ))
  ->removeComponent('field_image')
  ->save();

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.

Deprecated

as of Drupal 8.0.x, will be removed before Drupal 9.0.0. If the display is available in configuration use:

\Drupal::entityManager()
  ->getStorage('entity_view_display')
  ->load($entity_type . '.' . $bundle . '.' . $view_mode);

When the display is not available in configuration, you can create a new EntityViewDisplay object using:


  $values = array(
    'targetEntityType' => $entity_type,
    'bundle' => $bundle,
    'mode' => $view_mode,
    'status' => TRUE,
  ));
  \Drupal::entityManager()->getStorage('entity_view_display')->create($values);
  

See also

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

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

23 calls to entity_get_display()
BooleanFieldTest::testBooleanField in core/modules/field/src/Tests/Boolean/BooleanFieldTest.php
Tests boolean field.
CommentTestTrait::addDefaultCommentField in core/modules/comment/src/Tests/CommentTestTrait.php
Adds the default comment field to an entity.
EmailFieldTest::testEmailField in core/modules/field/src/Tests/Email/EmailFieldTest.php
Tests email field.
EntityCacheTagsTestBase::testReferencedEntity in core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php
Tests cache tags presence and invalidation of the entity when referenced.
EntityDisplayTest::testDeleteField in core/modules/field_ui/src/Tests/EntityDisplayTest.php
Tests deleting field.

... See full list

File

core/includes/entity.inc, line 462
Entity API for handling entities like nodes or users.

Code

function entity_get_display($entity_type, $bundle, $view_mode) {

  // Try loading the display from configuration.
  $display = entity_load('entity_view_display', $entity_type . '.' . $bundle . '.' . $view_mode);

  // If not found, create a fresh display object. We do not preemptively 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 = entity_create('entity_view_display', array(
      'targetEntityType' => $entity_type,
      'bundle' => $bundle,
      'mode' => $view_mode,
      'status' => TRUE,
    ));
  }
  return $display;
}