Drupal: Add node content into block depending on page ID


/ Published in: PHP
Save to your folder(s)

Fairly simple way to match sidebar content with the main content for several pages without the need to create multiple blocks or the hassle of putting content in a block.

For example, your Home, About and Contact pages all have a sidebar with some accompanying content which is different for each.

1. Create pages (nodes) for each of the sidebar content and note the node ID.
2. Add a block and paste in this code snippet
3. Edit the Switch/Case part of this snippet for your nodes
4. Enable the block for content/* and node/*


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3. * This code snippet is used to show selected node content
  4. * in the block based on the current node id. The idea is
  5. * to show relevant sidebar content that matches up with the
  6. * main content. One block instead of several.
  7. *
  8. * If logged in user has admin nodes then show an Edit link.
  9. */
  10. if ( arg(0) == "content" || arg(0) == "node" )
  11. {
  12. //Choose which node to put into the block
  13. switch (arg(1))
  14. {
  15. case "10": //nid of the home page
  16. $xNode = 58; //nid of content to load
  17. break;
  18. case "about": //this page is a view made with Views module so no nid
  19. $xNode = 59;
  20. break;
  21. case "40":
  22. $xNode = 69;
  23. break;
  24. default:
  25. return FALSE;
  26. }
  27.  
  28. $node = node_load($xNode);
  29.  
  30. //Add an edit link for admins
  31. if (user_access('administer nodes'))
  32. {
  33. $nodeurl = url('node/'. $node->nid);
  34. print('<a href="'.$nodeurl.'">[edit]</a>');
  35. }
  36.  
  37. $nodeout = node_view($node);
  38. print $nodeout;
  39.  
  40. }
  41.  
  42. /* // Helpful debugging info.
  43. $x=arg(0);
  44. $y=arg(1);
  45. $z=arg(1);
  46. print "<p>x=$x<br />y=$y<br />z=$z</p>";
  47. // ==End Debug== */
  48. ?>
  49. ‹ Simple book-like navigation using menu itemsupAggregator headline display with date ›

URL: http://drupal.org/node/273665

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.