function EntityDisplayRepositoryInterface::getViewDisplay

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityDisplayRepositoryInterface.php \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()
  2. 10 core/lib/Drupal/Core/Entity/EntityDisplayRepositoryInterface.php \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()
  3. 11.x core/lib/Drupal/Core/Entity/EntityDisplayRepositoryInterface.php \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()

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.

\Drupal::service('entity_display.repository')
  ->getViewDisplay('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: (optional) The view mode. Defaults to self::DEFAULT_DISPLAY_MODE.

Return value

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

1 method overrides EntityDisplayRepositoryInterface::getViewDisplay()
EntityDisplayRepository::getViewDisplay in core/lib/Drupal/Core/Entity/EntityDisplayRepository.php
Returns the entity view display associated with a bundle and view mode.

File

core/lib/Drupal/Core/Entity/EntityDisplayRepositoryInterface.php, line 149

Class

EntityDisplayRepositoryInterface
Provides an interface for an entity display repository.

Namespace

Drupal\Core\Entity

Code

public function getViewDisplay($entity_type, $bundle, $view_mode = self::DEFAULT_DISPLAY_MODE);

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.