Code Snippets

Creating an Area Plugin for Views

Here is an example, let's say you need to embed the site's contact form inside of a view's header. We'll name our module appnovation_views_contactformarea.

First, the same as anytime you need to provide a plugin for views, in your module file you need to implement hook_views_api():

1 2 3 4 5 6 7 8 9 /** * Implementation of hook_views_api(). */ function appnovation_views_contactformarea_views_api() { return array( 'api' => '3.0', 'path' => drupal_get_path('module', 'appnovation_views_contactformarea'), ); }

 

Then, again as with other plugins, create a file named MYMODULE.views.inc, and in there, we implement hook_views_data():

1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** * Implementation of hook_views_data(). */ function appnovation_views_contactformarea_views_data() { $data['views']['contactform'] = array( 'title' => t('Contact form'), 'help' => t("Displays the site's contact form."), 'area' => array( 'handler' => 'views_handler_area_contactform', ), ); return $data; }

The important part here is 'area', which is where we tell views that we have an area handler class and what the name of it is. The other keys in the array are used within the views UI. Next, create a file called the same as your area handler class name. In our case that is views_handler_area_contactform.inc.In it, provide the class extending the area handler of your choice, in our case we extend the standard views_handler_area:

1 2 3 4 5 6 7 8 9 10 11 class views_handler_area_contactform extends views_handler_area { function render($empty = FALSE) { if (!$empty || !empty($this->options['empty'])) { if (user_access('access site-wide contact form')) { module_load_include('inc', 'contact', 'contact.pages'); $contact_form = drupal_get_form('contact_site_form'); return drupal_render($contact_form); } } } }

For more information, refer to this blog post

Resource Tags: