Commit 97c3ee2b authored by webchick's avatar webchick

Issue #1875970 by yched, swentel: Pass EntityDisplay objects to the whole entity_view() callstack.

parent f2e25f60
......@@ -180,8 +180,11 @@ function hook_entity_query_alter(\Drupal\Core\Entity\Query\QueryInterface $query
/**
* Act on entities being assembled before rendering.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* entity components.
* @param $view_mode
* The view mode the entity is rendered in.
* @param $langcode
......@@ -196,12 +199,16 @@ function hook_entity_query_alter(\Drupal\Core\Entity\Query\QueryInterface $query
* @see hook_node_view()
* @see hook_user_view()
*/
function hook_entity_view(Drupal\Core\Entity\EntityInterface $entity, $view_mode, $langcode) {
$entity->content['my_additional_field'] = array(
'#markup' => $additional_field,
'#weight' => 10,
'#theme' => 'mymodule_my_additional_field',
);
function hook_entity_view(\Drupal\Core\Entity\EntityInterface $entity, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {
// Only do the extra work if the component is configured to be displayed.
// This assumes a 'mymodule_addition' extra field has been defined for the
// entity bundle in hook_field_extra_fields().
if ($display->getComponent('mymodule_addition')) {
$entity->content['mymodule_addition'] = array(
'#markup' => mymodule_addition($entity),
'#theme' => 'mymodule_my_additional_field',
);
}
}
/**
......@@ -221,6 +228,9 @@ function hook_entity_view(Drupal\Core\Entity\EntityInterface $entity, $view_mode
* A renderable array representing the entity content.
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity object being rendered.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* entity components.
*
* @see hook_entity_view()
* @see hook_comment_view_alter()
......@@ -228,7 +238,7 @@ function hook_entity_view(Drupal\Core\Entity\EntityInterface $entity, $view_mode
* @see hook_taxonomy_term_view_alter()
* @see hook_user_view_alter()
*/
function hook_entity_view_alter(&$build, Drupal\Core\Entity\EntityInterface $entity) {
function hook_entity_view_alter(&$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
$build['an_additional_field']['#weight'] = -10;
......@@ -245,17 +255,33 @@ function hook_entity_view_alter(&$build, Drupal\Core\Entity\EntityInterface $ent
* view. Only use this if attaching the data during the entity loading phase
* is not appropriate, for example when attaching other 'entity' style objects.
*
* @param array $entities
* The entities keyed by entity ID.
* @param string $entity_type
* The type of entities being viewed (i.e. node, user, comment).
* @param array $entities
* The entities keyed by entity ID.
* @param array $display
* The array of entity_display objects holding the display options configured
* for the entity components, keyed by bundle name.
* @param string $view_mode
* The view mode.
*/
function hook_entity_prepare_view($entities, $entity_type) {
function hook_entity_prepare_view($entity_type, array $entities, array $displays, $view_mode) {
// Load a specific node into the user object for later theming.
if (!empty($entities) && $entity_type == 'user') {
$nodes = mymodule_get_user_nodes(array_keys($entities));
foreach ($entities as $uid => $entity) {
$entity->user_node = $nodes[$uid];
// Only do the extra work if the component is configured to be
// displayed. This assumes a 'mymodule_addition' extra field has been
// defined for the entity bundle in hook_field_extra_fields().
$ids = array();
foreach ($entities as $id => $entity) {
if ($displays[$entity->bundle()]->getComponent('mymodule_addition')) {
$ids[] = $id;
}
}
if ($ids) {
$nodes = mymodule_get_user_nodes($ids);
foreach ($ids as $id) {
$entities[$id]->user_node = $nodes[$id];
}
}
}
}
......
......@@ -662,29 +662,6 @@ function entity_get_render_display(EntityInterface $entity, $view_mode) {
return $display;
}
/**
* Adjusts weights and visibility of components in displayed entities.
*
* This is used as a #pre_render callback.
*/
function _entity_view_pre_render($elements) {
$display = $elements['#entity_display'];
$extra_fields = field_info_extra_fields($display->targetEntityType, $display->bundle, 'display');
foreach (array_keys($extra_fields) as $name) {
if (isset($elements[$name]) && (!isset($elements[$name]['#access']) || $elements[$name]['#access'])) {
if ($options = $display->getComponent($name)) {
$elements[$name]['#weight'] = $options['weight'];
}
else {
$elements[$name]['#access'] = FALSE;
}
}
}
return $elements;
}
/**
* Returns the entity query object for this entity type.
*
......
......@@ -6,6 +6,7 @@
*/
namespace Drupal\Core\Entity;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
/**
* Base class for entity view controllers.
......@@ -26,54 +27,16 @@ public function __construct($entity_type) {
/**
* Implements Drupal\Core\Entity\EntityRenderControllerInterface::buildContent().
*/
public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
// Allow modules to change the view mode.
$context = array('langcode' => $langcode);
$view_modes = array();
$displays = array();
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) {
// Remove previously built content, if exists.
$entity->content = array();
drupal_alter('entity_view_mode', $view_mode, $entity, $context);
$entity->content['#view_mode'] = $view_mode;
$view_modes[$view_mode][$entity->id()] = $entity;
$bundle = $entity->bundle();
// Load the corresponding display settings if not stored yet.
if (!isset($displays[$view_mode][$bundle])) {
// Get the display object to use for rendering the entity..
$display = entity_get_render_display($entity, $view_mode);
// Let modules alter the display.
// Note: if config entities get a static cache at some point, the
// objects should be cloned before running drupal_alter().
$display_context = array(
'entity_type' => $this->entityType,
'bundle' => $bundle,
'view_mode' => $view_mode,
);
drupal_alter('entity_display', $display, $display_context);
$displays[$view_mode][$bundle] = $display;
}
// Assigning weights to 'extra fields' is done in a pre_render callback.
$entity->content['#pre_render'] = array('_entity_view_pre_render');
$entity->content['#entity_display'] = $displays[$view_mode][$bundle];
}
// Prepare and build field content, grouped by view mode.
foreach ($view_modes as $view_mode => $view_mode_entities) {
field_attach_prepare_view($this->entityType, $view_mode_entities, $displays[$view_mode], $langcode);
module_invoke_all('entity_prepare_view', $view_mode_entities, $this->entityType);
foreach ($view_mode_entities as $entity) {
$entity->content += field_attach_view($entity, $displays[$view_mode][$entity->bundle()], $langcode);
}
$entity->content = array(
'#view_mode' => $view_mode,
);
$entity->content += field_attach_view($entity, $displays[$entity->bundle()], $langcode);
}
}
......@@ -105,15 +68,18 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
*
* @param array $build
* The render array that is being created.
* @param Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be prepared.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for
* the entity components.
* @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.
*/
protected function alterBuild(array &$build, EntityInterface $entity, $view_mode, $langcode = NULL) { }
protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { }
/**
* Implements Drupal\Core\Entity\EntityRenderControllerInterface::view().
......@@ -130,26 +96,68 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
if (!isset($langcode)) {
$langcode = language(LANGUAGE_TYPE_CONTENT)->langcode;
}
$this->buildContent($entities, $view_mode, $langcode);
// 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);
}
$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;
module_invoke_all($view_hook, $entity, $entity_view_mode, $langcode);
module_invoke_all('entity_view', $entity, $entity_view_mode, $langcode);
$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);
$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);
$this->alterBuild($build[$key], $entity, $entity_view_mode, $langcode);
$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'];
}
}
$build[$key]['#weight'] = $weight++;
// Allow modules to modify the structured entity.
drupal_alter(array($view_hook, 'entity_view'), $build[$key], $entity);
// Allow modules to modify the render array.
drupal_alter(array($view_hook, 'entity_view'), $build[$key], $entity, $display);
}
return $build;
......
......@@ -11,13 +11,17 @@
* Defines a common interface for entity view controller classes.
*/
interface EntityRenderControllerInterface {
/**
* Build the structured $content property on the entity.
*
* @param array $entities
* The entities, implementing EntityInterface, whose content is being built.
* @param array $displays
* The array of entity_display objects holding the display options
* configured for the entity components, keyed by bundle name.
* @param string $view_mode
* (optional) The view mode that should be used to build the entity.
* The view mode in which the entity is being viewed.
* @param string $langcode
* (optional) For which language the entity should be build, defaults to
* the current content language.
......@@ -25,7 +29,7 @@ interface EntityRenderControllerInterface {
* @return array
* The content array.
*/
public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL);
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL);
/**
* Returns the render array for the provided entity.
......
......@@ -6,6 +6,7 @@
*/
use Drupal\node\Plugin\Core\Entity\Node;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
use Drupal\Core\Template\Attribute;
/**
......@@ -776,7 +777,7 @@ function book_node_load($nodes, $types) {
/**
* Implements hook_node_view().
*/
function book_node_view(Node $node, $view_mode) {
function book_node_view(Node $node, EntityDisplay $display, $view_mode) {
if ($view_mode == 'full') {
if (!empty($node->book['bid']) && empty($node->in_preview)) {
$node->content['book_navigation'] = array(
......
......@@ -64,8 +64,11 @@ function hook_comment_load(Drupal\comment\Comment $comments) {
/**
* Act on a comment that is being assembled before rendering.
*
* @param Drupal\comment\Comment $comment
* @param \Drupal\comment\Plugin\Core\Entity\Comment $comment $comment
* Passes in the comment the action is being performed on.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* comment components.
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
* @param $langcode
......@@ -73,9 +76,16 @@ function hook_comment_load(Drupal\comment\Comment $comments) {
*
* @see hook_entity_view()
*/
function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcode) {
// how old is the comment
$comment->time_ago = time() - $comment->changed;
function hook_comment_view(\Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {
// Only do the extra work if the component is configured to be displayed.
// This assumes a 'mymodule_addition' extra field has been defined for the
// node type in hook_field_extra_fields().
if ($display->getComponent('mymodule_addition')) {
$comment->content['mymodule_addition'] = array(
'#markup' => mymodule_addition($comment),
'#theme' => 'mymodule_my_additional_field',
);
}
}
/**
......@@ -93,13 +103,16 @@ function hook_comment_view(Drupal\comment\Comment $comment, $view_mode, $langcod
*
* @param $build
* A renderable array representing the comment.
* @param Drupal\comment\Comment $comment
* @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
* The comment being rendered.
* @param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* comment components.
*
* @see comment_view()
* @see hook_entity_view_alter()
*/
function hook_comment_view_alter(&$build, Drupal\comment\Comment $comment) {
function hook_comment_view_alter(&$build, \Drupal\comment\Plugin\Core\Entity\Comment $comment, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
// Check for the existence of a field added by another module.
if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
// Change its weight.
......
......@@ -10,6 +10,7 @@
*/
use Drupal\node\Plugin\Core\Entity\Node;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
use Drupal\file\Plugin\Core\Entity\File;
use Drupal\Core\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -566,7 +567,7 @@ function theme_comment_block($variables) {
/**
* Implements hook_node_view().
*/
function comment_node_view(Node $node, $view_mode) {
function comment_node_view(Node $node, EntityDisplay $display, $view_mode) {
$links = array();
if ($node->comment != COMMENT_NODE_HIDDEN) {
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRenderController;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
/**
* Render controller for comments.
......@@ -21,7 +22,7 @@ class CommentRenderController extends EntityRenderController {
* In addition to modifying the content key on entities, this implementation
* will also set the node key which all comments carry.
*/
public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
$return = array();
if (empty($entities)) {
return $return;
......@@ -30,7 +31,7 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
// Attach user account.
user_attach_accounts($entities);
parent::buildContent($entities, $view_mode, $langcode);
parent::buildContent($entities, $displays, $view_mode, $langcode);
// Load all nodes of all comments at once.
$nids = array();
......@@ -67,8 +68,8 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
/**
* Overrides Drupal\Core\Entity\EntityRenderController::alterBuild().
*/
protected function alterBuild(array &$build, EntityInterface $comment, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $comment, $view_mode, $langcode);
protected function alterBuild(array &$build, EntityInterface $comment, EntityDisplay $display, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $comment, $display, $view_mode, $langcode);
if (empty($comment->in_preview)) {
$prefix = '';
$is_threaded = isset($comment->divs)
......@@ -95,4 +96,5 @@ protected function alterBuild(array &$build, EntityInterface $comment, $view_mod
}
}
}
}
......@@ -18,19 +18,17 @@ class MessageRenderController extends EntityRenderController {
/**
* Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
*/
public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
parent::buildContent($entities, $view_mode, $langcode);
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
parent::buildContent($entities, $displays, $view_mode, $langcode);
foreach ($entities as $entity) {
// Add the message extra field, if enabled.
$entity_view_mode = $entity->content['#view_mode'];
$fields = field_extra_fields_get_display($entity, $entity_view_mode);
if (!empty($entity->message) && !empty($fields['message']['visible'])) {
$display = $displays[$entity->bundle()];
if (!empty($entity->message) && $display->getComponent('message')) {
$entity->content['message'] = array(
'#type' => 'item',
'#title' => t('Message'),
'#markup' => check_plain($entity->message),
'#weight' => $fields['message']['weight'],
);
}
}
......
......@@ -721,33 +721,6 @@ function field_view_mode_settings($entity_type, $bundle) {
return $cache[$entity_type][$bundle];
}
/**
* Returns the display options to use for pseudo-fields in a given view mode.
*
* @todo Remove when all steps in the view callstack receive the
* entity_display.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
* @param $view_mode
* The view mode.
*
* @return
* The display options to be used when viewing the entity's pseudo-fields in
* the view mode.
*/
function field_extra_fields_get_display(EntityInterface $entity, $view_mode) {
$entity_display = entity_get_render_display($entity, $view_mode);
$extra_fields = field_info_extra_fields($entity->entityType(), $entity->bundle(), 'display');
$options = array();
foreach ($extra_fields as $name => $value) {
$options[$name] = $entity_display->getComponent($name);
}
return $options;
}
/**
* Pre-render callback: Adjusts weights and visibility of non-field elements.
*/
......
......@@ -38,13 +38,6 @@ abstract class FormatterBase extends PluginSettingsBase implements FormatterInte
*/
protected $settings;
/**
* The formatter weight.
*
* @var int
*/
protected $weight;
/**
* The label display setting.
*
......@@ -71,20 +64,17 @@ abstract class FormatterBase extends PluginSettingsBase implements FormatterInte
* The field instance to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param int $weight
* The formatter weight.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
*/
public function __construct($plugin_id, DiscoveryInterface $discovery, $instance, array $settings, $weight, $label, $view_mode) {
public function __construct($plugin_id, DiscoveryInterface $discovery, $instance, array $settings, $label, $view_mode) {
parent::__construct(array(), $plugin_id, $discovery);
$this->instance = $instance;
$this->field = field_info_field($instance['field_name']);
$this->settings = $settings;
$this->weight = $weight;
$this->label = $label;
$this->viewMode = $view_mode;
}
......@@ -103,7 +93,6 @@ public function view(EntityInterface $entity, $langcode, array $items) {
$entity_type = $entity->entityType();
$info = array(
'#theme' => 'field',
'#weight' => $this->weight,
'#title' => $instance['label'],
'#access' => field_access('view', $field, $entity->entityType(), $entity),
'#label_display' => $this->label,
......
......@@ -19,6 +19,6 @@ class FormatterFactory extends DefaultFactory {
*/
public function createInstance($plugin_id, array $configuration) {
$plugin_class = $this->getPluginClass($plugin_id);
return new $plugin_class($plugin_id, $this->discovery, $configuration['instance'], $configuration['settings'], $configuration['weight'], $configuration['label'], $configuration['view_mode']);
return new $plugin_class($plugin_id, $this->discovery, $configuration['instance'], $configuration['settings'], $configuration['label'], $configuration['view_mode']);
}
}
......@@ -62,8 +62,6 @@ public function __construct() {
* requested formatter is not available.
* - settings: (array) Settings specific to the formatter. Each setting
* defaults to the default value specified in the formatter definition.
* - weight: (float) The weight to assign to the renderable element.
* Defaults to 0.
*
* @return \Drupal\field\Plugin\Type\Formatter\FormatterInterface
* A formatter object.
......@@ -113,7 +111,6 @@ public function prepareConfiguration($field_type, array $configuration) {
$configuration += array(
'label' => 'above',
'settings' => array(),
'weight' => 0,
);
// If no formatter is specified, use the default formatter.
if (!isset($configuration['type'])) {
......
......@@ -38,7 +38,6 @@ public function settingsForm(array $form, array &$form_state) {
$instance['display'][$this->viewMode] = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'weight' => $this->weight,
'label' => $this->label,
);
......@@ -62,7 +61,6 @@ public function settingsSummary() {
$instance['display'][$this->viewMode] = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'weight' => $this->weight,
'label' => $this->label,
);
......@@ -88,7 +86,6 @@ public function prepareView(array $entities, $langcode, array &$items) {
$display = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'weight' => $this->weight,
'label' => $this->label,
);
$displays = array();
......@@ -111,7 +108,6 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$display = array(
'type' => $this->getPluginId(),
'settings' => $this->getSettings(),
'weight' => $this->weight,
'label' => $this->label,
);
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Plugin\Core\Entity\Node;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
use Drupal\taxonomy\Plugin\Core\Entity\Term;
/**
......@@ -254,7 +255,7 @@ function _forum_node_check_node_type(Node $node) {
/**
* Implements hook_node_view().
*/
function forum_node_view(Node $node, $view_mode) {
function forum_node_view(Node $node, EntityDisplay $display, $view_mode) {
$vid = config('forum.settings')->get('vocabulary');
$vocabulary = taxonomy_vocabulary_load($vid);
if (_forum_node_check_node_type($node)) {
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRenderController;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
/**
* Render controller for nodes.
......@@ -18,7 +19,7 @@ class NodeRenderController extends EntityRenderController {
/**
* Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
*/
public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
$return = array();
if (empty($entities)) {
return $return;
......@@ -27,15 +28,16 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
// Attach user account.
user_attach_accounts($entities);
parent::buildContent($entities, $view_mode, $langcode);
parent::buildContent($entities, $displays, $view_mode, $langcode);
foreach ($entities as $key => $entity) {
$entity_view_mode = $entity->content['#view_mode'];
$bundle = $entity->bundle();
$display = $displays[$bundle];
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (node_hook($entity->bundle(), 'view')) {
$entity = node_invoke($entity, 'view', $entity_view_mode, $langcode);
if (node_hook($bundle, 'view')) {
$entity = node_invoke($entity, 'view', $display, $view_mode, $langcode);
}
$entity->content['links'] = array(
......@@ -47,7 +49,7 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
// Always display a read more link on teasers because we have no way
// to know when a teaser view is different than a full view.
$links = array();
if ($entity_view_mode == 'teaser') {
if ($view_mode == 'teaser') {
$node_title_stripped = strip_tags($entity->label());
$links['node-readmore'] = array(
'title' => t('Read more<span class="element-invisible"> about @title</span>', array(
......@@ -69,22 +71,23 @@ public function buildContent(array $entities = array(), $view_mode = 'full', $la
);
// Add Language field text element to node render array.
$entity->content['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#markup' => language_name($langcode),
'#weight' => 0,
'#prefix' => '<div id="field-language-display">',
'#suffix' => '</div>'
);
if ($display->getComponent('language')) {
$entity->content['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#markup' => language_name($langcode),
'#prefix' => '<div id="field-language-display">',
'#suffix' => '</div>'
);
}
}
}
/**
* Overrides Drupal\Core\Entity\EntityRenderController::alterBuild().
*/
protected function alterBuild(array &$build, EntityInterface $entity, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $entity, $view_mode, $langcode);
protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $entity, $display, $view_mode, $langcode);
// Add contextual links for this node, except when the node is already being
// displayed on its own page. Modules may alter this behavior (for example,
// to restrict contextual links to certain view modes) by implementing
......