NodeRenderController.php 2.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Definition of Drupal\node\NodeRenderController.
 */

namespace Drupal\node;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRenderController;
12
use Drupal\entity\Entity\EntityDisplay;
13 14 15 16 17 18 19 20 21

/**
 * Render controller for nodes.
 */
class NodeRenderController extends EntityRenderController {

  /**
   * Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
   */
22
  public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
23 24 25 26
    $return = array();
    if (empty($entities)) {
      return $return;
    }
27 28 29 30

    // Attach user account.
    user_attach_accounts($entities);

31
    parent::buildContent($entities, $displays, $view_mode, $langcode);
32

33
    foreach ($entities as $entity) {
34 35
      $bundle = $entity->bundle();
      $display = $displays[$bundle];
36 37 38 39 40 41 42 43 44 45

      $entity->content['links'] = array(
        '#theme' => 'links__node',
        '#pre_render' => array('drupal_pre_render_links'),
        '#attributes' => array('class' => array('links', 'inline')),
      );

      // 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();
46
      if ($view_mode == 'teaser') {
47 48
        $node_title_stripped = strip_tags($entity->label());
        $links['node-readmore'] = array(
49
          'title' => t('Read more<span class="visually-hidden"> about @title</span>', array(
50 51
            '@title' => $node_title_stripped,
          )),
52
          'href' => 'node/' . $entity->id(),
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
          'html' => TRUE,
          'attributes' => array(
            'rel' => 'tag',
            'title' => $node_title_stripped,
          ),
        );
      }

      $entity->content['links']['node'] = array(
        '#theme' => 'links__node__node',
        '#links' => $links,
        '#attributes' => array('class' => array('links', 'inline')),
      );

      // Add Language field text element to node render array.
68 69 70 71 72 73 74 75 76
      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>'
        );
      }
77 78 79 80 81 82
    }
  }

  /**
   * Overrides Drupal\Core\Entity\EntityRenderController::alterBuild().
   */
83 84
  protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) {
    parent::alterBuild($build, $entity, $display, $view_mode, $langcode);
85 86
    if ($entity->id()) {
      $build['#contextual_links']['node'] = array('node', array($entity->id()));
87
    }
88 89 90 91

    // The node 'submitted' info is not rendered in a standard way (renderable
    // array) so we have to add a cache tag manually.
    $build['#cache']['tags']['user'][] = $entity->uid;
92 93 94
  }

}