Created
November 28, 2014 12:45
-
-
Save DavidHernandez/859cbacdce8f2e892b67 to your computer and use it in GitHub Desktop.
render a node with comments when the $node item is not available in the path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* If you want to emulate the node/%node page in a page without the $node in the path | |
* you can just call the same page callback as that url: node_page_view($node); | |
*/ | |
$node = node_load($nid); | |
return node_page_view($node); | |
/* | |
* But, misteriously, the comments will not appear. | |
* | |
* A workaround will be to render the comments separately: | |
*/ | |
$node = node_load($nid); | |
$node_view = node_view($node); | |
$node_view->comments = comment_node_page_additions($node); | |
return $node_view; | |
/* | |
* this solution works, but is avoiding the implementation of hook_node_view of the comment | |
* module. So, it will always render the comments, even if the user doesn't have permissions. | |
* | |
* So, instead of forcing the render of the comments, we want to find why the comments are | |
* not rendered. In the implementation of hook_node_view of the comment page, on the last lines, | |
* does the next check: | |
* if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) { | |
* | |
* As you can see, it checks node_is_page(). This function internally uses menu_get_object(), | |
* as in our page we don't have the node object, the comments will not be rendered. | |
* | |
* So, we can fake in what page we are: | |
*/ | |
$node = node_load($nid); | |
$_GET['q'] = 'node/' . $nid; | |
$node_view = node_show($node); | |
return $node_view; | |
/* | |
* With this, we will have our comments working. But we will find a different issue. | |
* When a page callback returns a renderable array, Drupal calls the render function. | |
* While rendering a node, at some point drupal does a drupal_set_title. Overriding the title | |
* of our custom page. | |
* | |
* We can solve that rendering the node before and just returning the HTML in the page callback. | |
* And setting our title somewhere in the middle: | |
*/ | |
$node = node_load($nid); | |
$_GET['q'] = 'node/' . $nid; | |
$node_view = node_show($node); | |
$output = render($node_view); | |
drupal_set_title(t('Our custom title')); | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment