Categories

(83)
(69)
(8)
(34)
(74)
(149)

"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 (preprocess, hooks).

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

Internerdevels web development company appreciates your time.

6 votes, Rating: 5

Read also

1

Following the two previous articles about profilers, namely about XHprof profiler from facebook developers, we'll look at how it can possibly be  applied for analysing productivity of...

2

The IT industry practices encourage developers to keep improving their skills rather than...

3

CCK-formatters make up the code and are thus capable of displaying the fields the way a coder wants it. Coders often  come short of formatters, when performing tasks. In this...

4

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...

5

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...

Subscribe to our blog updates