1
votes

I'm trying to create a Drupal custom module that would enclose any page or article content with specific microdata tags (among other things). I've successfully inserted code in both the header and before the closing '' tag, but when I try to enclose the page content it just jams everything together at the end of the content.

For example, here's the module code:

function my_module_page_build(&$page) {

        // add manifest value to header
        $manifest_code = "..."
        $page['header']['manifest'] = array(
            "#weight" => 0,
            "#markup" => t($manifest_code)
            );
        // add opening microdata tags
        $opening_microdata = "...";
        $page['content']['my_module_open_microdata'] = array(
            "#weight" => 0,
            "#markup" => t($opening_microdata)
            );
        $closing_microdata = "...";
        $page['content']['my_module_close_microdata'] = array(
            "#weight" => 25,
            "#markup" => t($closing_microdata)
            );
        // add javascript to footer
        $javascript = "...";
        $page['page_bottom']['my_module_javascript'] = array(
            "#weight" => 0,
            "#markup" => t($javascript)
            );


}

I don't want to insert the closing microdata into the $page['page-bottom'] slot because I don't want some of these links and other code inside the microdata tags. I've also tried using a negative weight to move it up, but no luck.

I've tried searching for more information on Node or Blog or Article structure to see if there are other slots for the code, but I can't find anything.

I'm sure this is very easy -- are there resources I'm missing for understanding page structure? Some simple solution I'm missing?

1

1 Answers

2
votes

Two potential solutions:

  1. Try the tip from the first comment on hook_page_build, which should make your weight declarations actually take effect. (Not sure if resetting the #sorted flag might have adverse effects, though.)

  2. Switch from using hook_page_build() to your_module_preprocess_page(&$variables), adding your microdata tags and other stuff as custom entries to the ´$variables´ array there. Then adjust your page template to place these additional variables as you please. This gives you pretty decent control over the placement of your additions, at the price of being tied to theme customizations (i.e. it works well for custom site building, but not so well if your module is intended for general usage with arbitrary themes).