Commit 9c54978d authored by catch's avatar catch

Issue #1987778 by monan, tim.plunkett, damiankloip, Crell, jessebeach, scor,...

Issue #1987778 by monan, tim.plunkett, damiankloip, Crell, jessebeach, scor, vijaycs85, jibran, kim.pepper, dawehner, dokumori: Convert node_show() and node_page_view() to a new style controller.
parent 46db1365
......@@ -10,6 +10,7 @@
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity\Entity\EntityDisplay;
/**
* Entities changed before this time are always shown as read.
......@@ -128,6 +129,30 @@ function history_cron() {
->execute();
}
/**
* Implements hook_node_view_alter().
*/
function history_node_view_alter(&$build, EntityInterface $node, EntityDisplay $display) {
// Update the history table, stating that this user viewed this node.
if (($display->originalMode === 'full') && \Drupal::currentUser()->isAuthenticated()) {
$build['#attached'] = array(
'js' => array(
// When the window's "load" event is triggered, mark the node as read.
// This still allows for Drupal behaviors (which are triggered on the
// "DOMContentReady" event) to add "new" and "updated" indicators.
array(
'data' => 'window.addEventListener("load",function(){Drupal.history.markAsRead(' . $node->id() . ');},false);',
'type' => 'inline',
),
),
'library' => array(
array('history', 'drupal.history'),
),
);
}
}
/**
* Implements hook_node_delete().
*/
......
......@@ -7,13 +7,15 @@
namespace Drupal\node\Controller;
use Drupal\Component\Utility\String;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
/**
* Returns responses for Node routes.
*/
class NodeController {
class NodeController extends ControllerBase {
/**
* @todo Remove node_admin_nodes().
......@@ -40,11 +42,34 @@ public function add(EntityInterface $node_type) {
}
/**
* @todo Remove node_show().
* Displays a node revision.
*
* @param int $node_revision
* The node revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow($node_revision) {
$node_revision = entity_revision_load('node', $node_revision);
return node_show($node_revision, TRUE);
$node = $this->entityManager()->getStorageController('node')->loadRevision($node_revision);
$page = $this->buildPage($node);
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) {
$node = $this->entityManager()->getStorageController('node')->loadRevision($node_revision);
return $this->t('Revision of %title from %date', array('%title' => $node->label(), '%date' => format_date($node->getRevisionCreationTime())));
}
/**
......@@ -55,4 +80,66 @@ public function revisionOverview(NodeInterface $node) {
return node_revision_overview($node);
}
/**
* Displays a node.
*
* @param \Drupal\node\NodeInterface $node
* The node we are displaying.
*
* @return array
* An array suitable for drupal_render().
*/
public function page(NodeInterface $node) {
$build = $this->buildPage($node);
foreach ($node->uriRelationships() as $rel) {
$uri = $node->uri($rel);
// Set the node path as the canonical URL to prevent duplicate content.
$build['#attached']['drupal_add_html_head_link'][] = array(
array(
'rel' => $rel,
'href' => $this->urlGenerator()->generateFromPath($uri['path'], $uri['options']),
)
, TRUE);
if ($rel == 'canonical') {
// Set the non-aliased canonical path as a default shortlink.
$build['#attached']['drupal_add_html_head_link'][] = array(
array(
'rel' => 'shortlink',
'href' => $this->urlGenerator()->generateFromPath($uri['path'], array_merge($uri['options'], array('alias' => TRUE))),
)
, TRUE);
}
}
return $build;
}
/**
* The _title_callback for the node.view route.
*
* @param NodeInterface $node
* The current node.
*
* @return string
* The page title.
*/
public function pageTitle(NodeInterface $node) {
return String::checkPlain($node->label());
}
/**
* Builds a node page render array.
*
* @param \Drupal\node\NodeInterface $node
* The node we are displaying.
*
* @return array
* An array suitable for drupal_render().
*/
protected function buildPage(NodeInterface $node) {
return array('nodes' => $this->entityManager()->getRenderController('node')->view($node));
}
}
<?php
/**
* @file
* Contains \Drupal\node\Controller\NodeView.
*/
namespace Drupal\node\Controller;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\NodeInterface;
/**
* Returns responses for Node routes.
*/
class NodeView {
/**
* @todo Remove node_page_view().
*/
public function page(NodeInterface $node) {
return node_page_view($node);
}
/**
* The _title_callback for the node.view route.
*
* @param \Drupal\node\NodeInterface $node
*/
public function pageTitle(NodeInterface $node) {
return String::checkPlain($node->label());
}
}
<?php
/**
* @file
* Contains \Drupal\node\Tests\NodeViewTest.
*/
namespace Drupal\node\Tests;
/**
* Tests the node/{node} page.
*
* @see \Drupal\node\Controller\NodeController
*/
class NodeViewTest extends NodeTestBase {
public static function getInfo() {
return array(
'name' => 'Node view page',
'description' => 'Tests the node/{node} page.',
'group' => 'Node',
);
}
/**
* Tests the html head links.
*/
public function testHtmlHeadLinks() {
$node = $this->drupalCreateNode();
$uri = $node->uri();
$this->drupalGet($uri['path']);
$result = $this->xpath('//link[@rel = "version-history"]');
$this->assertEqual($result[0]['href'], url("node/{$node->id()}/revisions"));
$result = $this->xpath('//link[@rel = "edit-form"]');
$this->assertEqual($result[0]['href'], url("node/{$node->id()}/edit"));
$result = $this->xpath('//link[@rel = "canonical"]');
$this->assertEqual($result[0]['href'], url("node/{$node->id()}"));
}
}
......@@ -563,54 +563,6 @@ function node_revision_delete($revision_id) {
entity_revision_delete('node', $revision_id);
}
/**
* Page callback: Generates an array which displays a node detail page.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* A node entity.
* @param $message
* (optional) A flag which sets a page title relevant to the revision being
* viewed. Default is FALSE.
*
* @return
* A $page element suitable for use by drupal_render().
*
* @see node_menu()
*
* @deprecated Use \Drupal\node\Controller\NodeController::revisionShow()
*/
function node_show(EntityInterface $node, $message = FALSE) {
// For markup consistency with other pages, use node_view_multiple() rather than node_view().
$page = array('nodes' => node_view_multiple(array($node->id() => $node), 'full'));
if ($message) {
$page['#title'] = t('Revision of %title from %date', array('%title' => $node->label(), '%date' => format_date($node->getRevisionCreationTime())));
// Don't use the render cache when a revision is displayed.
unset($page['nodes'][$node->id()]['#cache']);
}
// Update the history table, stating that this user viewed this node.
global $user;
if (\Drupal::moduleHandler()->moduleExists('history') && $user->isAuthenticated()) {
$page['#attached'] = array(
'js' => array(
// When the window's "load" event is triggered, mark the node as read.
// This still allows for Drupal behaviors (which are triggered on the
// "DOMContentReady" event) to add "new" and "updated" indicators.
array(
'data' => 'window.addEventListener("load",function(){Drupal.history.markAsRead(' . $node->id() . ');},false);',
'type' => 'inline',
),
),
'library' => array(
array('history', 'drupal.history'),
)
);
}
return $page;
}
/**
* Checks whether the current page is the full page view of the passed-in node.
*
......@@ -1457,36 +1409,6 @@ function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) {
return entity_view_multiple($nodes, $view_mode, $langcode);
}
/**
* Page callback: Displays a single node.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* The node entity.
*
* @return
* A page array suitable for use by drupal_render().
*
* @see node_menu()
*
* @deprecated Use \Drupal\node\Controller\NodeController::viewPage()
*/
function node_page_view(EntityInterface $node) {
foreach ($node->uriRelationships() as $rel) {
$uri = $node->uri($rel);
// Set the node path as the canonical URL to prevent duplicate content.
drupal_add_html_head_link(array('rel' => $rel, 'href' => url($uri['path'], $uri['options'])), TRUE);
if ($rel == 'canonical') {
// Set the non-aliased canonical path as a default shortlink.
drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);
}
}
$build = node_show($node);
return $build;
}
/**
* Implements hook_form_FORM_ID_alter().
*
......
......@@ -41,8 +41,8 @@ node.add:
node.view:
path: '/node/{node}'
defaults:
_content: '\Drupal\node\Controller\NodeView::page'
_title_callback: '\Drupal\node\Controller\NodeView::pageTitle'
_content: '\Drupal\node\Controller\NodeController::page'
_title_callback: '\Drupal\node\Controller\NodeController::pageTitle'
requirements:
_entity_access: 'node.view'
......@@ -64,8 +64,8 @@ node.revision_overview:
node.revision_show:
path: '/node/{node}/revisions/{node_revision}/view'
defaults:
_title: 'Revisions'
_content: '\Drupal\node\Controller\NodeController::revisionShow'
_title_callback: '\Drupal\node\Controller\NodeController::revisionPageTitle'
requirements:
_access_node_revision: 'view'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment