2

I'm trying to add a piece on content to the $page['content'] variable, but it's just not working and the examples are not helping me. My code is the following:

function my_modulename_page_alter(&$page) {
    $page['content'] = $extracontent . $page['content'];
}

dpm() doesn't allow me to see the available variables. What am I doing wrong?

3 Answers 3

5

Try using hook_page_build(&$page). hook_page_alter(&$page), which is called directly after it, is meant for themers to use after all the modules are finished with their business.

I just implemented function MODULE_page_alter(&$page), and it worked in the module as expected.
Did you clear the cache so the module is registered?

3
  • 3
    hook_page_alter() is thought to be used from modules too; the difference is that hook_page_alter() runs after hook_page_build(), and it is used to add elements at the page level that depend on another module's elements. Using hook_page_build() would not resolve the question being asked: What does $page['content'] contain?
    – apaderno
    May 16, 2011 at 23:02
  • 1
    Thanks, clearing the cache was the kicker. I didn't know it had anything to do with module processing.
    – J-Fiz
    May 17, 2011 at 3:25
  • Also, drush cc registry will clear the module_implements() cache -- this means your new hooks will be found by Drupal -- and is usually a lot faster than a whole cache clear. Sep 22, 2011 at 20:24
3

$page['content'] is not a string, but an array that contains many indexes. As reported in hook_page_alter(), the 'content' element contains the main content of the current page, and its structure will vary depending on what module is responsible for building the page.

When the page is build from the Node and Blog modules, $page['content'] contains:

  • $page['content']['system_main']['nodes'][$nid]['body']: the node body
  • $page['content']['system_main']['nodes'][$nid]['links']: the array of links attached to the node
  • $page['content']['system_main']['nodes'][$nid]['#node']: the node object
  • $page['content']['system_main']['pager']: the pager for the results.
0

For me, it's accessed at $page['content']['system_main']['nodes'][$nid][<fieldname>]. I've been wondering if it can also be accessed with hook_node_view_alter().

As for dpm(), it does 2 weird things for me:

2
  • This doesn't answer what $page['content'] contains.
    – apaderno
    Aug 15, 2011 at 7:18
  • I've had problems using dpm() on the $page array as well. I believe it has to do with the fact that, after the first time you do it, you're dpm()ing a page with dpm() output already in it (in the messages block), and this just blows up Kruomo (no idea why). Sep 22, 2011 at 20:26

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.