Entity view (Content)

How to create an area plugin for views

By manuel
Twitter
Feb. 11, 2015

Sometimes using views, you need to place some dynamic content in the header or footer of a view. You may be tempted to just write some PHP inside the header of a view, but well, you know that using PHP as configuration for a view is wrong. Since in my previous post I did not cover this use case, I thought I'd write a blog post about it, to keep you from falling into the dark side ;)

OK, as 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():

/**
 * 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():

/**
 * 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:

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);
      }
    }
  }
}

Keep in mind that we do not make use in our example of any options, but in the same way you provide options for your custom fields, you do the same here by implementing the methods: option_definition(), options_form(), options_submit(), etc. You can take a look inside views module's handlers directory for some examples on this.

And that's it, the render() method is what gets called when the view is rendered (duh), so just do your magic in there. And remember that you can implement your own methods within the class to organize your code properly, in the likely case that you are doing something more complex than our example. And if you want some inspiration, take a look at Crell's handy views_linkarea module, pretty cool, don't you think?

Post Tags: