NodeController.php 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\node\Controller\NodeController.
 */

namespace Drupal\node\Controller;

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Utility\Xss;
12
use Drupal\Core\Controller\ControllerBase;
13
use Drupal\Core\Datetime\DateFormatter;
14
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
15
use Drupal\node\NodeTypeInterface;
16
use Drupal\node\NodeInterface;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18 19 20 21

/**
 * Returns responses for Node routes.
 */
22 23 24
class NodeController extends ControllerBase implements ContainerInjectionInterface {

  /**
25
   * The date formatter service.
26
   *
27
   * @var \Drupal\Core\Datetime\DateFormatter
28
   */
29
  protected $dateFormatter;
30 31 32 33

  /**
   * Constructs a NodeController object.
   *
34
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
35
   *   The date formatter service.
36
   */
37 38
  public function __construct(DateFormatter $date_formatter) {
    $this->dateFormatter = $date_formatter;
39 40 41 42 43 44
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
45
    return new static($container->get('date.formatter'));
46 47
  }

48 49

  /**
50 51 52 53 54 55 56 57 58 59 60
   * Displays add content links for available content types.
   *
   * Redirects to node/add/[type] if only one content type is available.
   *
   * @return array
   *   A render array for a list of the node types that can be added; however,
   *   if there is only one node type defined for the site, the function
   *   redirects to the node add page for that one node type and does not return
   *   at all.
   *
   * @see node_menu()
61 62
   */
  public function addPage() {
63 64 65
    $content = array();

    // Only use node types the user has access to.
66
    foreach ($this->entityManager()->getStorage('node_type')->loadMultiple() as $type) {
67
      if ($this->entityManager()->getAccessControlHandler('node')->createAccess($type->type)) {
68 69 70 71 72 73 74
        $content[$type->type] = $type;
      }
    }

    // Bypass the node/add listing if only one content type is available.
    if (count($content) == 1) {
      $type = array_shift($content);
75
      return $this->redirect('node.add', array('node_type' => $type->type));
76 77 78 79 80 81
    }

    return array(
      '#theme' => 'node_add_list',
      '#content' => $content,
    );
82 83 84
  }

  /**
85 86 87 88 89 90 91
   * Provides the node submission form.
   *
   * @param \Drupal\node\NodeTypeInterface $node_type
   *   The node type entity for the node.
   *
   * @return array
   *   A node submission form.
92
   */
93 94 95 96
  public function add(NodeTypeInterface $node_type) {
    $account = $this->currentUser();
    $langcode = $this->moduleHandler()->invoke('language', 'get_default_langcode', array('node', $node_type->type));

97
    $node = $this->entityManager()->getStorage('node')->create(array(
98 99 100
      'uid' => $account->id(),
      'name' => $account->getUsername() ?: '',
      'type' => $node_type->type,
101
      'langcode' => $langcode ? $langcode : $this->languageManager()->getCurrentLanguage()->id,
102 103
    ));

104
    $form = $this->entityFormBuilder()->getForm($node);
105 106

    return $form;
107 108 109
  }

  /**
110 111 112 113 114 115 116
   * Displays a node revision.
   *
   * @param int $node_revision
   *   The node revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
117 118
   */
  public function revisionShow($node_revision) {
119
    $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
120 121
    $node_view_controller = new NodeViewController($this->entityManager);
    $page = $node_view_controller->view($node);
122 123 124 125 126 127 128 129 130 131 132 133 134 135
    unset($page['nodes'][$node->id()]['#cache']);
    return $page;
  }

  /**
   * Page title callback for a node revision.
   *
   * @param int $node_revision
   *   The node revision ID.
   *
   * @return string
   *   The page title.
   */
  public function revisionPageTitle($node_revision) {
136
    $node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
137
    return $this->t('Revision of %title from %date', array('%title' => $node->label(), '%date' => format_date($node->getRevisionCreationTime())));
138 139 140
  }

  /**
141 142 143 144 145 146 147
   * Generates an overview table of older revisions of a node.
   *
   * @param \Drupal\node\NodeInterface $node
   *   A node object.
   *
   * @return array
   *   An array as expected by drupal_render().
148 149
   */
  public function revisionOverview(NodeInterface $node) {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    $account = $this->currentUser();
    $node_storage = $this->entityManager()->getStorage('node');
    $type = $node->getType();

    $build = array();
    $build['#title'] = $this->t('Revisions for %title', array('%title' => $node->label()));
    $header = array($this->t('Revision'), $this->t('Operations'));

    $revert_permission = (($account->hasPermission("revert $type revisions") || $account->hasPermission('revert all revisions') || $account->hasPermission('administer nodes')) && $node->access('update'));
    $delete_permission =  (($account->hasPermission("delete $type revisions") || $account->hasPermission('delete all revisions') || $account->hasPermission('administer nodes')) && $node->access('delete'));

    $rows = array();

    $vids = $node_storage->revisionIds($node);

    foreach (array_reverse($vids) as $vid) {
      if ($revision = $node_storage->loadRevision($vid)) {
        $row = array();

        $revision_author = $revision->uid->entity;

        if ($vid == $node->getRevisionId()) {
          $username = array(
            '#theme' => 'username',
            '#account' => $revision_author,
          );
176
          $row[] = array('data' => $this->t('!date by !username', array('!date' => $this->l($this->dateFormatter->format($revision->revision_timestamp->value, 'short'), 'entity.node.canonical', array('node' => $node->id())), '!username' => drupal_render($username)))
177
            . (($revision->revision_log->value != '') ? '<p class="revision-log">' . Xss::filter($revision->revision_log->value) . '</p>' : ''),
178 179 180 181 182 183 184 185
            'class' => array('revision-current'));
          $row[] = array('data' => String::placeholder($this->t('current revision')), 'class' => array('revision-current'));
        }
        else {
          $username = array(
            '#theme' => 'username',
            '#account' => $revision_author,
          );
186
          $row[] = $this->t('!date by !username', array('!date' => $this->l($this->dateFormatter->format($revision->revision_timestamp->value, 'short'), 'node.revision_show', array('node' => $node->id(), 'node_revision' => $vid)), '!username' => drupal_render($username)))
187
            . (($revision->revision_log->value != '') ? '<p class="revision-log">' . Xss::filter($revision->revision_log->value) . '</p>' : '');
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

          if ($revert_permission) {
            $links['revert'] = array(
              'title' => $this->t('Revert'),
              'route_name' => 'node.revision_revert_confirm',
              'route_parameters' => array('node' => $node->id(), 'node_revision' => $vid),
            );
          }

          if ($delete_permission) {
            $links['delete'] = array(
              'title' => $this->t('Delete'),
              'route_name' => 'node.revision_delete_confirm',
              'route_parameters' => array('node' => $node->id(), 'node_revision' => $vid),
            );
          }

          $row[] = array(
            'data' => array(
              '#type' => 'operations',
              '#links' => $links,
            ),
          );
        }

        $rows[] = $row;
      }
    }

    $build['node_revisions_table'] = array(
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
      '#attached' => array(
        'library' => array('node/drupal.node.admin'),
      ),
    );

    return $build;
  }

229 230 231 232 233 234 235 236 237 238
  /**
   * The _title_callback for the node.add route.
   *
   * @param \Drupal\node\NodeTypeInterface $node_type
   *   The current node.
   *
   * @return string
   *   The page title.
   */
  public function addPageTitle(NodeTypeInterface $node_type) {
239
    return $this->t('Create @name', array('@name' => $node_type->name));
240 241
  }

242
}