Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityRenderController.
Angie Byron
committed
*/
namespace Drupal\Core\Entity;
Angie Byron
committed
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
Angie Byron
committed
/**
* Base class for entity view controllers.
*/
class EntityRenderController implements EntityRenderControllerInterface {
/**
* The type of entities for which this controller is instantiated.
*
* @var string
*/
protected $entityType;
public function __construct($entity_type) {
$this->entityType = $entity_type;
}
/**
* Implements \Drupal\Core\Entity\EntityRenderControllerInterface::buildContent().
Angie Byron
committed
*/
Angie Byron
committed
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
field_attach_prepare_view($this->entityType, $entities, $displays, $langcode);
module_invoke_all('entity_prepare_view', $this->entityType, $entities, $displays, $view_mode);
foreach ($entities as $entity) {
Angie Byron
committed
// Remove previously built content, if exists.
Angie Byron
committed
$entity->content = array(
'#view_mode' => $view_mode,
);
$entity->content += field_attach_view($entity, $displays[$entity->bundle()], $langcode);
Angie Byron
committed
}
}
/**
* Provides entity-specific defaults to the build process.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
Angie Byron
committed
* The entity for which the defaults should be provided.
* @param string $view_mode
* The view mode that should be used.
* @param string $langcode
* (optional) For which language the entity should be prepared, defaults to
* the current content language.
*
* @return array
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode) {
$return = array(
'#theme' => $this->entityType,
"#{$this->entityType}" => $entity,
'#view_mode' => $view_mode,
'#langcode' => $langcode,
);
return $return;
}
/**
* Specific per-entity building.
*
* @param array $build
* The render array that is being created.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityInterface $entity
Angie Byron
committed
* The entity to be prepared.
Angie Byron
committed
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for
* the entity components.
Angie Byron
committed
* @param string $view_mode
* The view mode that should be used to prepare the entity.
* @param string $langcode
* (optional) For which language the entity should be prepared, defaults to
* the current content language.
*/
Angie Byron
committed
protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { }
Angie Byron
committed
/**
* Implements \Drupal\Core\Entity\EntityRenderControllerInterface::view().
Angie Byron
committed
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
$buildList = $this->viewMultiple(array($entity), $view_mode, $langcode);
return $buildList[0];
}
/**
* Implements \Drupal\Core\Entity\EntityRenderControllerInterface::viewMultiple().
Angie Byron
committed
*/
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = language(LANGUAGE_TYPE_CONTENT)->langcode;
}
Angie Byron
committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Build the view modes and display objects.
$view_modes = array();
$displays = array();
$context = array('langcode' => $langcode);
foreach ($entities as $entity) {
$bundle = $entity->bundle();
// Allow modules to change the view mode.
$entity_view_mode = $view_mode;
drupal_alter('entity_view_mode', $entity_view_mode, $entity, $context);
// Store entities for rendering by view_mode.
$view_modes[$entity_view_mode][$entity->id()] = $entity;
// Load the corresponding display settings if not stored yet.
if (!isset($displays[$entity_view_mode][$bundle])) {
// Get the display object for this bundle and view mode.
$display = entity_get_render_display($entity, $entity_view_mode);
// Let modules alter the display.
$display_context = array(
'entity_type' => $this->entityType,
'bundle' => $bundle,
'view_mode' => $entity_view_mode,
);
drupal_alter('entity_display', $display, $display_context);
$displays[$entity_view_mode][$bundle] = $display;
}
}
foreach ($view_modes as $mode => $view_mode_entities) {
$this->buildContent($view_mode_entities, $displays[$mode], $mode, $langcode);
}
Angie Byron
committed
$view_hook = "{$this->entityType}_view";
$build = array('#sorted' => TRUE);
$weight = 0;
foreach ($entities as $key => $entity) {
$entity_view_mode = isset($entity->content['#view_mode']) ? $entity->content['#view_mode'] : $view_mode;
Angie Byron
committed
$display = $displays[$entity_view_mode][$entity->bundle()];
module_invoke_all($view_hook, $entity, $display, $entity_view_mode, $langcode);
module_invoke_all('entity_view', $entity, $display, $entity_view_mode, $langcode);
Angie Byron
committed
$build[$key] = $entity->content;
// We don't need duplicate rendering info in $entity->content.
unset($entity->content);
$build[$key] += $this->getBuildDefaults($entity, $entity_view_mode, $langcode);
Angie Byron
committed
$this->alterBuild($build[$key], $entity, $display, $entity_view_mode, $langcode);
// Assign the weights configured in the display.
foreach ($display->getComponents() as $name => $options) {
if (isset($build[$key][$name])) {
$build[$key][$name]['#weight'] = $options['weight'];
}
}
Angie Byron
committed
$build[$key]['#weight'] = $weight++;
Angie Byron
committed
// Allow modules to modify the render array.
drupal_alter(array($view_hook, 'entity_view'), $build[$key], $entity, $display);
Angie Byron
committed
}
return $build;
}
}