Sunday, February 20, 2011

Adding an email form to a Drupal View

I recently needed to add a form to the top of a view. Never having to do that before, I dove into the world of Views handlers (of which, there are way too many). I chose-

hook_views_pre_render(&$view)

At this stage, you receive a view object right before it is rendered on the screen. However, this applies to all views, so I needed to specify the specific view/display. Selecting the view was easy-

if ($view->vid == %d)
* %d == the required view id

However, I struggled on selecting just the display I wanted to affect. After lots of googling, I found the answer-

if ($view->vid == %d && $view->current_display == %s)
* %s == page_1, page_2, block_1, etc

After that, it was a matter of setting-

$view->attachment_before

equal to my form. All done!

Given my initial impression of Views, the object and handlers could really use some simplification. However, I'm sure there's a reason for all of it that I'm just not aware of yet (please feel free to drop some knowledge in the comments).

No comments:

Post a Comment