Wednesday, December 18, 2013

Putting content on a specific part of a Drupal page

I wanted to add some buttons to my page. The best place looked like it was on the right hand side of the menu bar. That way I wouldn't waste any vertical space and the buttons only needed to be small and inconspicuous (they were for adding and deleting annotations). The problem was how to do it. You couldn't do that by just assigning the text to an existing region so I defined a new one. I called it "Annotations". In my theme's .info file I added the line:

regions[annotator] = Annotations

But you could call it anything you like. The next step was to create a module that could generate the content. I created a trivial "annotator" module that had a hook_block_view function. This generated some content when the module was being rendered by the template.

So to get the module called I had to install and enable it. Then in the Structure section of admin I told Drupal to assign the annotator module's output to the "Annotations" region.

Finally, I edited the template (as it turned out the specific template for that page, but it could be the general one) in my theme so that just after it has rendered the menu it then tests for the existence of the "annotator" module and renders the content:

<?php if ($page['annotator']) : ?>
    if (module_exists('annotator'))
        print drupal_render($page['annotator']);
<?php endif; ?>

All that remains to be done is to style the module output so that it floats to the right and Bob's your uncle. The next step will be to hide them from casual browsers since only logged-in users are supposed to be able to annotate.

No comments:

Post a Comment