annotate core/modules/node/src/NodeViewBuilder.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\node;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Chris@0 6 use Drupal\Core\Entity\EntityInterface;
Chris@0 7 use Drupal\Core\Entity\EntityViewBuilder;
Chris@0 8 use Drupal\node\Entity\Node;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * View builder handler for nodes.
Chris@0 12 */
Chris@0 13 class NodeViewBuilder extends EntityViewBuilder {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * {@inheritdoc}
Chris@0 17 */
Chris@0 18 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
Chris@0 19 /** @var \Drupal\node\NodeInterface[] $entities */
Chris@0 20 if (empty($entities)) {
Chris@0 21 return;
Chris@0 22 }
Chris@0 23
Chris@0 24 parent::buildComponents($build, $entities, $displays, $view_mode);
Chris@0 25
Chris@0 26 foreach ($entities as $id => $entity) {
Chris@0 27 $bundle = $entity->bundle();
Chris@0 28 $display = $displays[$bundle];
Chris@0 29
Chris@0 30 if ($display->getComponent('links')) {
Chris@0 31 $build[$id]['links'] = [
Chris@0 32 '#lazy_builder' => [
Chris@0 33 get_called_class() . '::renderLinks', [
Chris@0 34 $entity->id(),
Chris@0 35 $view_mode,
Chris@0 36 $entity->language()->getId(),
Chris@0 37 !empty($entity->in_preview),
Chris@0 38 ],
Chris@0 39 ],
Chris@0 40 ];
Chris@0 41 }
Chris@0 42
Chris@0 43 // Add Language field text element to node render array.
Chris@0 44 if ($display->getComponent('langcode')) {
Chris@0 45 $build[$id]['langcode'] = [
Chris@0 46 '#type' => 'item',
Chris@0 47 '#title' => t('Language'),
Chris@0 48 '#markup' => $entity->language()->getName(),
Chris@0 49 '#prefix' => '<div id="field-language-display">',
Chris@0 50 '#suffix' => '</div>'
Chris@0 51 ];
Chris@0 52 }
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
Chris@0 60 $defaults = parent::getBuildDefaults($entity, $view_mode);
Chris@0 61
Chris@0 62 // Don't cache nodes that are in 'preview' mode.
Chris@0 63 if (isset($defaults['#cache']) && isset($entity->in_preview)) {
Chris@0 64 unset($defaults['#cache']);
Chris@0 65 }
Chris@0 66
Chris@0 67 return $defaults;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * #lazy_builder callback; builds a node's links.
Chris@0 72 *
Chris@0 73 * @param string $node_entity_id
Chris@0 74 * The node entity ID.
Chris@0 75 * @param string $view_mode
Chris@0 76 * The view mode in which the node entity is being viewed.
Chris@0 77 * @param string $langcode
Chris@0 78 * The language in which the node entity is being viewed.
Chris@0 79 * @param bool $is_in_preview
Chris@0 80 * Whether the node is currently being previewed.
Chris@0 81 *
Chris@0 82 * @return array
Chris@0 83 * A renderable array representing the node links.
Chris@0 84 */
Chris@0 85 public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) {
Chris@0 86 $links = [
Chris@0 87 '#theme' => 'links__node',
Chris@0 88 '#pre_render' => ['drupal_pre_render_links'],
Chris@0 89 '#attributes' => ['class' => ['links', 'inline']],
Chris@0 90 ];
Chris@0 91
Chris@0 92 if (!$is_in_preview) {
Chris@0 93 $entity = Node::load($node_entity_id)->getTranslation($langcode);
Chris@0 94 $links['node'] = static::buildLinks($entity, $view_mode);
Chris@0 95
Chris@0 96 // Allow other modules to alter the node links.
Chris@0 97 $hook_context = [
Chris@0 98 'view_mode' => $view_mode,
Chris@0 99 'langcode' => $langcode,
Chris@0 100 ];
Chris@0 101 \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
Chris@0 102 }
Chris@0 103 return $links;
Chris@0 104 }
Chris@0 105
Chris@0 106 /**
Chris@0 107 * Build the default links (Read more) for a node.
Chris@0 108 *
Chris@0 109 * @param \Drupal\node\NodeInterface $entity
Chris@0 110 * The node object.
Chris@0 111 * @param string $view_mode
Chris@0 112 * A view mode identifier.
Chris@0 113 *
Chris@0 114 * @return array
Chris@0 115 * An array that can be processed by drupal_pre_render_links().
Chris@0 116 */
Chris@0 117 protected static function buildLinks(NodeInterface $entity, $view_mode) {
Chris@0 118 $links = [];
Chris@0 119
Chris@0 120 // Always display a read more link on teasers because we have no way
Chris@0 121 // to know when a teaser view is different than a full view.
Chris@0 122 if ($view_mode == 'teaser') {
Chris@0 123 $node_title_stripped = strip_tags($entity->label());
Chris@0 124 $links['node-readmore'] = [
Chris@0 125 'title' => t('Read more<span class="visually-hidden"> about @title</span>', [
Chris@0 126 '@title' => $node_title_stripped,
Chris@0 127 ]),
Chris@0 128 'url' => $entity->urlInfo(),
Chris@0 129 'language' => $entity->language(),
Chris@0 130 'attributes' => [
Chris@0 131 'rel' => 'tag',
Chris@0 132 'title' => $node_title_stripped,
Chris@0 133 ],
Chris@0 134 ];
Chris@0 135 }
Chris@0 136
Chris@0 137 return [
Chris@0 138 '#theme' => 'links__node__node',
Chris@0 139 '#links' => $links,
Chris@0 140 '#attributes' => ['class' => ['links', 'inline']],
Chris@0 141 ];
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * {@inheritdoc}
Chris@0 146 */
Chris@0 147 protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
Chris@0 148 /** @var \Drupal\node\NodeInterface $entity */
Chris@0 149 parent::alterBuild($build, $entity, $display, $view_mode);
Chris@0 150 if ($entity->id()) {
Chris@0 151 if ($entity->isDefaultRevision()) {
Chris@0 152 $build['#contextual_links']['node'] = [
Chris@0 153 'route_parameters' => ['node' => $entity->id()],
Chris@0 154 'metadata' => ['changed' => $entity->getChangedTime()],
Chris@0 155 ];
Chris@0 156 }
Chris@0 157 else {
Chris@0 158 $build['#contextual_links']['node_revision'] = [
Chris@0 159 'route_parameters' => [
Chris@0 160 'node' => $entity->id(),
Chris@0 161 'node_revision' => $entity->getRevisionId(),
Chris@0 162 ],
Chris@0 163 'metadata' => ['changed' => $entity->getChangedTime()],
Chris@0 164 ];
Chris@0 165 }
Chris@0 166 }
Chris@0 167 }
Chris@0 168
Chris@0 169 }