annotate core/modules/node/src/NodeViewBuilder.php @ 19:fa3358dc1485 tip

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