January 31, 2008

Displaying Drupal subpages on Parent Pages

I am the webmaster of WSU's ACM Chapter.  Since past webpages for our ACM site has been done by hand in HTML, it is hard to update or add information.  It is even harder to teach a future webmaster the ropes, and what will eventually happens it that it gets neglected.  It is for also many other reasons that the ACM Club at our school isn't doing so well, but that's information for another post.  Since I am the "official" webmaster for our new website, I have decided that it is a lot wiser to use some sort of CMS (Content Management System).  Due to a variety of reasons, I've opted to use Drupal.

drupal-subpages.gifIn the course of creating content for the website in Drupal, I've created a series of Parent Pages as well as subpages belonging to them.  As you can see on this image, my parent page is Officer Information.  Once the page is clicked, we'll see subpages related to the parent page.  This is fine and dandy, but the content of the parent page would not contain this information.  What this essentially means is that everytime create a subpage for a new officer, you would have to edit the Officer Information page, add information, so on and so forth.

What's the big deal?  Well, not only is it time consuming, but it makes it easier to introduce errors or inconsistent information to the website.  So, after doing research for a few hours and came up fruitless, I've decided to write my own little script to do exactly what I needed.  That is, to print the information of the subpage inside of the parent page.  Let me give you the code, and then I'll quickly explain what it means.  I know this isn't the written code in the world, but it does what I need it to do.

[code lang="php"] $display) { if ($display['path'] == "node/$node") { $pid = $key; break;cd } } $output = '

'; foreach ($menu['visible'] as $page) { if ($page['pid'] == $pid) { $output .= "$page[title]
"; } } echo $output; ?> [/code]

Of course, the first requirement is that change the "Input format" of the post to "PHP code".  Next, you need to change the number 3 in the first line of code to the node number of the parent page.  That's it!  Now, when the parent page is loaded, all the information of the subpages will automatically appear.  It may be necessary for you to change the way Drupal displays its URL.  You want the search engine friendly types of URLs for this to work.

Let's see what the script does.  The first foreach loop goes through the multi-dimensional array to look for the key associated with the parent node.  It then uses this key in the second loop and search for all pages whose pid (parent id) is equal to the parent id that was found earlier.  When found, it just makes a hyperlink and concatenates it to the variable $output.  Once all is done, it prints it out!  Easy as pie :)

Filed under Blog, Web Development by A.K.

Spread the Word!

Permalink Print Comment

Leave a Comment