Chris@0: $entity) {
Chris@0: $bundle = $entity->bundle();
Chris@0: $display = $displays[$bundle];
Chris@0:
Chris@0: if ($display->getComponent('links')) {
Chris@0: $build[$id]['links'] = [
Chris@0: '#lazy_builder' => [
Chris@0: get_called_class() . '::renderLinks', [
Chris@0: $entity->id(),
Chris@0: $view_mode,
Chris@0: $entity->language()->getId(),
Chris@0: !empty($entity->in_preview),
Chris@14: $entity->isDefaultRevision() ? NULL : $entity->getLoadedRevisionId(),
Chris@0: ],
Chris@0: ],
Chris@0: ];
Chris@0: }
Chris@0:
Chris@0: // Add Language field text element to node render array.
Chris@0: if ($display->getComponent('langcode')) {
Chris@0: $build[$id]['langcode'] = [
Chris@0: '#type' => 'item',
Chris@0: '#title' => t('Language'),
Chris@0: '#markup' => $entity->language()->getName(),
Chris@0: '#prefix' => '
',
Chris@17: '#suffix' => '
',
Chris@0: ];
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * {@inheritdoc}
Chris@0: */
Chris@0: protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
Chris@0: $defaults = parent::getBuildDefaults($entity, $view_mode);
Chris@0:
Chris@0: // Don't cache nodes that are in 'preview' mode.
Chris@0: if (isset($defaults['#cache']) && isset($entity->in_preview)) {
Chris@0: unset($defaults['#cache']);
Chris@0: }
Chris@0:
Chris@0: return $defaults;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * #lazy_builder callback; builds a node's links.
Chris@0: *
Chris@0: * @param string $node_entity_id
Chris@0: * The node entity ID.
Chris@0: * @param string $view_mode
Chris@0: * The view mode in which the node entity is being viewed.
Chris@0: * @param string $langcode
Chris@0: * The language in which the node entity is being viewed.
Chris@0: * @param bool $is_in_preview
Chris@0: * Whether the node is currently being previewed.
Chris@14: * @param $revision_id
Chris@14: * (optional) The identifier of the node revision to be loaded. If none
Chris@14: * is provided, the default revision will be loaded.
Chris@0: *
Chris@0: * @return array
Chris@0: * A renderable array representing the node links.
Chris@0: */
Chris@14: public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview, $revision_id = NULL) {
Chris@0: $links = [
Chris@0: '#theme' => 'links__node',
Chris@0: '#pre_render' => ['drupal_pre_render_links'],
Chris@0: '#attributes' => ['class' => ['links', 'inline']],
Chris@0: ];
Chris@0:
Chris@0: if (!$is_in_preview) {
Chris@14: $storage = \Drupal::entityTypeManager()->getStorage('node');
Chris@14: /** @var \Drupal\node\NodeInterface $revision */
Chris@14: $revision = !isset($revision_id) ? $storage->load($node_entity_id) : $storage->loadRevision($revision_id);
Chris@14: $entity = $revision->getTranslation($langcode);
Chris@0: $links['node'] = static::buildLinks($entity, $view_mode);
Chris@0:
Chris@0: // Allow other modules to alter the node links.
Chris@0: $hook_context = [
Chris@0: 'view_mode' => $view_mode,
Chris@0: 'langcode' => $langcode,
Chris@0: ];
Chris@0: \Drupal::moduleHandler()->alter('node_links', $links, $entity, $hook_context);
Chris@0: }
Chris@0: return $links;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Build the default links (Read more) for a node.
Chris@0: *
Chris@0: * @param \Drupal\node\NodeInterface $entity
Chris@0: * The node object.
Chris@0: * @param string $view_mode
Chris@0: * A view mode identifier.
Chris@0: *
Chris@0: * @return array
Chris@0: * An array that can be processed by drupal_pre_render_links().
Chris@0: */
Chris@0: protected static function buildLinks(NodeInterface $entity, $view_mode) {
Chris@0: $links = [];
Chris@0:
Chris@0: // Always display a read more link on teasers because we have no way
Chris@0: // to know when a teaser view is different than a full view.
Chris@0: if ($view_mode == 'teaser') {
Chris@0: $node_title_stripped = strip_tags($entity->label());
Chris@0: $links['node-readmore'] = [
Chris@0: 'title' => t('Read more about @title', [
Chris@0: '@title' => $node_title_stripped,
Chris@0: ]),
Chris@18: 'url' => $entity->toUrl(),
Chris@0: 'language' => $entity->language(),
Chris@0: 'attributes' => [
Chris@0: 'rel' => 'tag',
Chris@0: 'title' => $node_title_stripped,
Chris@0: ],
Chris@0: ];
Chris@0: }
Chris@0:
Chris@0: return [
Chris@0: '#theme' => 'links__node__node',
Chris@0: '#links' => $links,
Chris@0: '#attributes' => ['class' => ['links', 'inline']],
Chris@0: ];
Chris@0: }
Chris@0:
Chris@0: }