Returns the entity_display object associated to 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_display object 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:
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();
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.
\Drupal\entity\Plugin\Core\Entity\EntityDisplay The display object associated to the view mode.
function entity_get_display($entity_type, $bundle, $view_mode) {
// Try loading the display from configuration.
$display = entity_load('entity_display', $entity_type . '.' . $bundle . '.' . $view_mode);
// If not found, create a fresh display object. We do not preemptively create
// new entity_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_display', array(
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $view_mode,
));
}
return $display;
}