Categories

(111)
(64)
(165)
(21)

"Display extender" plugin creation for Views

06.06.2012
Author:

Not all Drupal developers know that the Views module provides a pretty wide API. In addition to a big hooks system, the functionality of views can be expanded with use of some additional plugins. There are 10 types of plugins to be applied to views. In the current article we will examine creation of a Display extender type plugin for the views, which facilitates adding your own settings to the display. Further on, these settings can be utilized in whatever place there's a need for the display to be called in.

So as to have the plugin realized we shall create a module, and announce  hook_views_api() in it:

php
/** 
 * Implements hook_views_api(). 
 */ 
function extender_example_views_api() { 
  return array( 
    'api' => 3, 
  ); 
} 

Next, following the description in the documentation, we ought to call  hook_views_plugins() in the extender_example.views.inc  file:

php
/** 
 * Implements hook_views_plugins(). 
 */ 
function extender_example_views_plugins() { 
  $path = drupal_get_path('module', 'extender_example');
  $plugins = array(); 
  $plugins['display_extender']['extender_example'] = array( 
    'title' => t('Extender example textarea'), 
    'help' => t('Add textarea.'), 
    'path' => $path, 
    'handler' => 'extender_example_plugin_display_extender_code',
  ); 
  return $plugins; 
} 

Well, here's the plugin class itself which extends the class views_plugin_display_extender:

php
/** 
 * The plugin that added additional setting to views edit form.
 * 
 * @ingroup views_display_plugins 
 */ 
class extender_example_plugin_display_extender_code extends views_plugin_display_extender {
  /** 
   * Provide a form to edit options for this plugin. 
   */ 
  function option_definition(&$options) { 
    $options['extender_example'] = array('default' => '');
  } 

  /** 
   * Provide the form to set new option. 
   */ 
  function options_form(&$form, &$form_state) { 
    switch ($form_state['section']) { 
      case 'extender_example': 
        $form['#title'] .= t('Example setting'); 
        $form['extender_example'] = array( 
          '#type' => 'textarea', 
          '#description' => t('Custom text which will display whenever you want'),
          '#default_value' => $this->display->get_option('extender_example'), 
        ); 
        break; 
    } 
  } 
   
  /** 
   * Inserts the code into the view display. 
   */ 
  function options_submit(&$form, &$form_state) { 
    $new_option  = $form_state['values']['extender_example']; 
    switch ($form_state['section']) { 
      case 'extender_example': 
        $this->display->set_option('extender_example', $new_option); 
        $empty = trim($new_option); 
        $empty = empty($empty); 
        break; 
    } 
  } 

  /** 
   * Summarizes new option. 
   * 
   * Lists the fields as either 'Yes' if there is text or 'None' otherwise and
   * categorizes the fields under the 'Other' category. 
   */ 
  function options_summary(&$categories, &$options) { 
    $new_option = check_plain(trim($this->display->get_option('extender_example'))); 
    if ($new_option) { 
      $new_option = t('Yes'); 
    } 
    else { 
      $new_option = t('None'); 
    } 
    $options['extender_example'] = array( 
      'category' => 'other', 
      'title'    => t('Extender example'), 
      'value'    => $new_option, 
      'desc'     => t('Add some option.'), 
    ); 
  } 
} 

In fact, all you will need to have is a simple plugin for views to be written. The new settings can be removed from the views object: $view->display['page']->display_options['extender_example']  

anywhere where it is available (preproces, hooks).

More detailed description of  this plugin imlemenation procedure can be found in module  Code per Views Display (btw, it inspired me, too).

I appreciate your time.

5 votes, Rating: 5

Read also

1

Following the two previous articles (thisthat) about...

2

CCK-formatters make up the code and are thus...

3

Popups have come so much into fashion, of late, and thus appealed to customers' sentiment that the public are starting growing more and more convinced: an ideal site has to be in popups. Whareas...

4

In the previous post I gave an example of Ctools modal API as being operated with a single form.In this one you'll be given an insight...

5
Very often there is a need to develop a site with multi-language support - possibility to translate site content on different languages. It's quite simple to do this task on Drupal, and such modules...
Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime