13

I would like to show a node inside another node. So I retrieve the node id of the second node and I would like to render/print/... it inside another node.
But I'm not sure how to do this. I've tried drupal_render, node_view, print,... but with no results. Any advice?

$nid = $node->field_linked_fiche['und'][0]['nid'];
$fullFiche = node_load($nid);

EDIT - the whole template

<?php

    /**
     * @file
     * Bartik's theme implementation to display a single Drupal page.
     *
     ...
     */
    ?>
    <div id="page-wrapper"><div id="page">

      <div id="header" class="<?php print $secondary_menu ? 'with-secondary-menu': 'without-secondary-menu'; ?>"><div class="section clearfix">

        <?php if ($logo): ?>
          <a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home" id="logo">
            <img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" />
          </a>
        <?php endif; ?>

        <?php if ($site_name || $site_slogan): ?>
          <div id="name-and-slogan"<?php if ($hide_site_name && $hide_site_slogan) { print ' class="element-invisible"'; } ?>>

            <?php if ($site_name): ?>
              <?php if ($title): ?>
                <div id="site-name"<?php if ($hide_site_name) { print ' class="element-invisible"'; } ?>>
                  <strong>
                    <a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home"><span><?php print $site_name; ?></span></a>
                  </strong>
                </div>
              <?php else: /* Use h1 when the content title is empty */ ?>
                <h1 id="site-name"<?php if ($hide_site_name) { print ' class="element-invisible"'; } ?>>
                  <a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>" rel="home"><span><?php print $site_name; ?></span></a>
                </h1>
              <?php endif; ?>
            <?php endif; ?>

            <?php if ($site_slogan): ?>
              <div id="site-slogan"<?php if ($hide_site_slogan) { print ' class="element-invisible"'; } ?>>
                <?php print $site_slogan; ?>
              </div>
            <?php endif; ?>

          </div> <!-- /#name-and-slogan -->
        <?php endif; ?>

        <?php print render($page['header']); ?>

        <?php if ($main_menu): ?>
          <div id="main-menu" class="navigation">
            <?php print theme('links__system_main_menu', array(
              'links' => $main_menu,
              'attributes' => array(
                'id' => 'main-menu-links',
                'class' => array('links', 'clearfix'),
              ),
              'heading' => array(
                'text' => t('Main menu'),
                'level' => 'h2',
                'class' => array('element-invisible'),
              ),
            )); ?>
          </div> <!-- /#main-menu -->
        <?php endif; ?>

        <?php if ($secondary_menu): ?>
          <div id="secondary-menu" class="navigation">
            <?php print theme('links__system_secondary_menu', array(
              'links' => $secondary_menu,
              'attributes' => array(
                'id' => 'secondary-menu-links',
                'class' => array('links', 'inline', 'clearfix'),
              ),
              'heading' => array(
                'text' => t('Secondary menu'),
                'level' => 'h2',
                'class' => array('element-invisible'),
              ),
            )); ?>
          </div> <!-- /#secondary-menu -->
        <?php endif; ?>

      </div></div> <!-- /.section, /#header -->

      <?php if ($messages): ?>
        <div id="messages"><div class="section clearfix">
          <?php print $messages; ?>
        </div></div> <!-- /.section, /#messages -->
      <?php endif; ?>

      <div id="main-wrapper" class="clearfix"><div id="main" class="clearfix">

        <?php if ($breadcrumb): ?>
          <div id="breadcrumb"><?php print $breadcrumb; ?></div>
        <?php endif; ?>

        <?php if ($page['sidebar_first']): ?>
          <div id="sidebar-first" class="column sidebar"><div class="section">
            <?php print render($page['sidebar_first']); ?>
          </div></div> <!-- /.section, /#sidebar-first -->
        <?php endif; ?>

        <div id="content" class="column"><div class="section">
          <?php if ($page['highlighted']): ?><div id="highlighted"><?php print render($page['highlighted']); ?></div><?php endif; ?>
          <a id="main-content"></a>
          <?php print render($title_prefix); ?>
          <?php if ($title): ?>
            <h1 class="title" id="page-title">
              <?php print $title; ?>
            </h1>
          <?php endif; ?>
          <?php print render($title_suffix); ?>
          <?php if ($tabs): ?>
            <div class="tabs">
              <?php print render($tabs); ?>
            </div>
          <?php endif; ?>
          <?php print render($page['help']); ?>
          <?php if ($action_links): ?>
            <ul class="action-links">
              <?php print render($action_links); ?>
            </ul>
          <?php endif; ?>
          // Render second node inside this one
          <?php 
              $nid = 739; // NOTE :: Hard coded, but need to be variable!!
              drupal_render(node_view(node_load($nid)))
          ?>
          <?php print render($page['content']); ?>
          <?php print $feed_icons; ?>

        </div></div> <!-- /.section, /#content -->

        <?php if ($page['sidebar_second']): ?>
          <div id="sidebar-second" class="column sidebar"><div class="section">
            <?php print render($page['sidebar_second']); ?>
          </div></div> <!-- /.section, /#sidebar-second -->
        <?php endif; ?>

      </div></div> <!-- /#main, /#main-wrapper -->

      <div id="footer-wrapper"><div class="section">

        <?php if ($page['footer']): ?>
          <div id="footer" class="clearfix">
            <?php print render($page['footer']); ?>
          </div> <!-- /#footer -->
        <?php endif; ?>

      </div></div> <!-- /.section, /#footer-wrapper -->

    </div></div> <!-- /#page, /#page-wrapper -->
apaderno
  • 28,547
  • 16
  • 75
  • 90
Michiel
  • 7,855
  • 16
  • 61
  • 113
  • I didn't test it but it should work with drupal_render(node_view(node_load($nid))) – Marius Ilie Dec 19 '11 at 16:37
  • 1
    No, I told you I already tried this... – Michiel Dec 19 '11 at 16:39
  • I guess you did something wrong because I did the same thing ( `drupal_render(node_view(node_load($nid)))` ) to display multiple nodes on the same page and it worked perfectly. – Marius Ilie Dec 19 '11 at 16:51
  • And you did it in your page.tpl? – Michiel Dec 19 '11 at 18:29
  • I'm curious how you did it... The script you just posted didn't do anything in my `page.tpl.php`. I get this error: `Strict warning: Only variables should be passed by reference in include() (line 9 of ***` – Michiel Dec 19 '11 at 19:39
  • Maybe a version mismatch? In Drupal 6, `node_view()` returns the themed node (i.e. it calls `drupal_render()` internally), so it gives already rendered and 'ready to use' markup. In Drupal 7, `node_view()` only prepares the structured array needed by `drupal_render()` to generate the markup. – Henrik Opel Dec 19 '11 at 22:56
  • I'm using Drupal 7. So which snippet do I need? – Michiel Dec 20 '11 at 05:28
  • In Drupal 7, you'd need the snippet posted by Marius Ilie (`drupal_render(node_view(node_load($nid)))`). If it does not work for you, you should verify your preconditions and the in between steps, i.e. do you have a valid nid, does node_load() return a valid node object, etc... – Henrik Opel Dec 20 '11 at 08:22
  • I have a valid $nid (a `var_dump` shows me all the aspects of the node) and so does the `node_load()`. But I keep getting the variable error mentioned above... – Michiel Dec 20 '11 at 08:28
  • 1
    I did it in a module, not in the page.tpl.php but the syntax should be the same. Also, I first loaded the node then render it: `$node2 = node_load_multiple($nids);` `$output = drupal_render(node_view($node2));` – Marius Ilie Dec 20 '11 at 08:46
  • I just tried it, but I must be doing something terible wrong, cause still no results appear... – Michiel Dec 20 '11 at 09:31
  • can I see the code you're using? I mean the whole template – Marius Ilie Dec 20 '11 at 10:18
  • It's just a standard `page.tpl.php`, with your code added... – Michiel Dec 20 '11 at 10:28
  • 1
    you forgot to print the rendered content :) – Marius Ilie Dec 20 '11 at 10:33
  • BTW, I flagged the deleted answer by Marius Ilie for moderator attention, requesting to revert the delete, as it looks like it was the correct answer. (I can see deleted answers, but can not undelete them myself). – Henrik Opel Dec 20 '11 at 11:17

3 Answers3

60
<?php
  $nid = 739; // NOTE :: Hard coded, but need to be variable!!
  print drupal_render(node_view(node_load($nid)));
?>

NOTE: Reposted, as original answer got deleted.

Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
Marius Ilie
  • 3,283
  • 2
  • 25
  • 27
5

drupal 7
https://api.drupal.org/api/drupal/includes!common.inc/function/render/7
https://api.drupal.org/api/drupal/modules!node!node.module/function/node_view/7

<?php //use variable to avoid strict warning
$nid = 739;
$nodeview = node_view(node_load($nid));
print drupal_render($nodeview);
?>
Gregory.K
  • 71
  • 2
  • 3
5

I didn't test it but it should work with

drupal_render(node_view(node_load($nid)))
Scott Boettger
  • 3,657
  • 2
  • 33
  • 44
Marius Ilie
  • 3,283
  • 2
  • 25
  • 27
  • I guess you did something wrong because I did the same thing ( `drupal_render(node_view(node_load($nid)))` ) to display multiple nodes on the same page and it worked perfectly. – Marius Ilie Dec 19 '11 at 16:51
  • And you did it in your page.tpl? – Michiel Dec 19 '11 at 18:29
  • I'm curious how you did it... The script you just posted didn't do anything in my `page.tpl.php`. I get this error: `Strict warning: Only variables should be passed by reference in include() (line 9 of ***` – Michiel Dec 19 '11 at 19:39
  • 2
    Maybe a version mismatch? In Drupal 6, `node_view()` returns the themed node (i.e. it calls `drupal_render()` internally), so it gives already rendered and 'ready to use' markup. In Drupal 7, `node_view()` only prepares the structured array needed by `drupal_render()` to generate the markup. – Henrik Opel Dec 19 '11 at 22:56
  • +1 to remove the undesired negative score (probably not worth bothering the moderator again for re-deletion ;) – Henrik Opel Dec 20 '11 at 14:09