I'm using THEME_page_alter to give my site a meta description, and is working fine. However I can't find the variable which defines the content type - I need to use it as an argument to give different pages different descriptions. I tried $node->type

Side question: where on earth can I find answers for such simple tasks like this without having to ask in DA? I did a lot of Googling and looking in the API, and looking at the Globals list... but it seems like the official Drupal documentation doesn't make things easy for you in some respects.



hook_page_alter() is invoked for every output page for which drupal_render_page() is invoked; $node is not a parameter passed to the hook, nor is there such global variable.

As reported in the documentation, the structure of a page that is output from the Node content contains some specific elements; in particular, it contains the array $page['content']['system_main']['nodes'], which has an element for each shown node. For every node, the array contains:

  • "body": the body of the node
  • "links": the links attached to the node
  • "#node": the node object

The code to handle that array should be something similar to the following one:

if (!empty($page['content']['system_main']['nodes']) && is_array($page['content']['system_main']['nodes'])) {
  foreach ($page['content']['system_main']['nodes'] as $nid => $content) {
    if ($content['#node']->type == 'The content type you are interested to') {
      // Append to $page['content']['system_main']['nodes'][$nid]['body'] the meta information you want to add.

Using a test module I wrote, and the Devel module, I noticed that the structure of $page['content']['system_main']['nodes'] is the following one:


The structure for the "body" index is the following one:


To append a string to the body, you should append it to $page['content']['system_main']['nodes'][$nid]['body'][0]['#markup'].

  • Great I have it working now, although I still don't quite understand how it all works despite your clear explanation - need to read through the documentation again! Thank you. – Mythical Fish Sep 18 '11 at 6:57
  • So how do I now access the node content for use in the description? Again I've looked everywhere but no luck. From your explanation I would have thought it would be $content['body'] – Mythical Fish Sep 18 '11 at 12:26
  • To access it to write it, you should use $page['content']['system_main']['nodes'][$nid]['body']. Using $content['body'] would not set it, in the code I reported; it would set it if the loop were foreach ($page['content']['system_main']['nodes'] as $nid => &$content) {}. – kiamlaluno Sep 18 '11 at 16:30
  • Ok the only trouble I'm having now is that the frontpage is using the node data for the description! I imagine its due to the blocks I'm using. How do I differentiate my frontpage from my node pages? – Mythical Fish Sep 20 '11 at 19:16
  • "#view_mode" should be "teaser" when the node is shown in the front page; when the node is shown in "node/[nid]" "#view_mode" should be "full." – kiamlaluno Sep 20 '11 at 21:03

Im new to Drupal. Finding variables and displaying them the way i wanted was very important to me. So i used a code that works in any php script. But i recommend you to always to stick with the Drupal API.

Here is the magic code.

<pre><?php var_dump(get_defined_vars()); ?></pre>

This will output all the available variables for the current page. However the page will be very long so don do this on your live production site. Test it on a local site or put your site in maintenance mode.

Also check the http://www.hoomanb.com/cs/QuickRef/drupal-7-theming-cheat-sheet.pdf

  • That cheat sheet is pretty handy, bookmarked it, cheers. – Mythical Fish Sep 18 '11 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.