You are here

function entity_get_display in Drupal 8

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

in drupal:8.8.0 and is removed from drupal:9.0.0. Use EntityDisplayRepositoryInterface::getViewDisplay() instead.

See also

https://www.drupal.org/node/2835616

1 call to entity_get_display()
EntityLegacyTest::testLegacyDisplayFunctions in core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php
@expectedDeprecation entity_get_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \Drupal::service('entity_display.repository')->getViewDisplay() instead. See…

File

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

Code

function entity_get_display($entity_type, $bundle, $view_mode) {
  @trigger_error('entity_get_display() is deprecated in drupal:8.8.0. It will be removed before drupal:9.0.0. Use \\Drupal::service(\'entity_display.repository\')->getViewDisplay() instead. See https://www.drupal.org/node/2835616', E_USER_DEPRECATED);
  return \Drupal::service('entity_display.repository')
    ->getViewDisplay($entity_type, $bundle, $view_mode);
}