The Drupal module Views possesses an API for embedding plug-ins. The many plug-ins available can easily be seen by using views_discover_plugins(). Various plug-ins exist for:
- Access
- Argument default
- Argument validator
- Cache
- Display
- Style, i.e. list, table, or RSS
Despite the abundant areas covered it is rather easy to develop plug-ins once one has understood the basics, allowing for quite complex extensions. Though many complain about Views HTML, it is not at all difficult to define a task. You can even build your own rights layer determined by your specific needs.
Do not confuse plug-ins with handlers, the latter building SQL queries, hence working directly on the data. Plug-ins, on the other hand, operate only indirectly upon the resulting data.
As an example on what Views plug-ins can do, let’s take a look at how to evaluate two specific arguments retrievable from the URL which cannot be treated via Panels or Views; we need to find a Node and a TermID. Views includes some very good examples on how to program plug-ins; everything starts with a call to hook_views_api().
<?php
function hook_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'example') . '/includes/views',
);
}
?>Now that we have introduced our module to Views we set up further hooks; the 'path' variable here is optional, it’s just to keep everything transparent. One way to proceed is with hook_views_plugins(); best to explore /includes/plugins.inc. In our example case we are interested in the standard arguments.
<?php
function views_views_plugins() {
$plugins = array(
'argument default' => array(
// This type of plugin does not conform to the standard and
// uses 'fixed' as the parent rather than having a separate parent.
'fixed' => array(
'title' => t('Fixed entry'),
'handler' => 'views_plugin_argument_default',
),
'php' => array(
'title' => t('PHP Code'),
'handler' => 'views_plugin_argument_default_php',
'parent' => 'fixed',
),
),
);
}
?>Just select that part of the array you need and adjust it according to your plug-in’s name.
I believe only title and handler are needed. views_discover_plugins() shows how the array is used in principle: the handler is just the name of the file and the class of our plug-ins. You can see this in /plugins/views_plugin_argument_default.inc, which shows all files with their respective class definitions for the plug-ins.
<?php
// $Id: views_plugin_argument_default.inc,v 1.2 2009/06/01 23:34:55 merlinofchaos Exp $
/**
* @file
* Contains the fixed argument default plugin.
*/
/**
* @defgroup views_argument_default_plugins Views' argument default plugins
* @{
*
* Allow specialized methods of filling in arguments when they aren't
* provided.
*
* @see hook_views_plugins
*/
/**
* The fixed argument default handler; also used as the base.
*/
class views_plugin_argument_default extends views_plugin {
var $option_name = 'default_argument_fixed';
/**
* Initialize this plugin with the view and the argument
* it is linked to.
*/
function init(&$view, &$argument, $id = NULL) {
$this->view = &$view;
$this->argument = &$argument;
$this->id = $id;
}
/**
* Determine if the administrator has the privileges to use this
* plugin
*/
function access() { return TRUE; }
function argument_form(&$form, &$form_state) {
$form[$this->option_name] = array(
'#type' => 'textfield',
'#title' => t('Default argument'),
'#default_value' => $this->get_argument(),
'#process' => array('views_process_dependency'),
'#dependency' => array(
'radio:options[default_action]' => array('default'),
'radio:options[default_argument_type]' => array($this->id)
),
'#dependency_count' => 2,
);
// Only do this if using one simple standard form gadget
$this->check_access($form);
}
/**
* If we don't have access to the form but are showing it anyway, ensure that
* the form is safe and cannot be changed from user input.
*/
function check_access(&$form) {
if (!$this->access()) {
$form[$this->option_name]['#disabled'] = TRUE;
$form[$this->option_name]['#value'] = $form[$this->option_name]['#default_value'];
$form[$this->option_name]['#description'] .= ' <strong>' . t('Note: you do not have permission to modify this. If you change the default argument type, this setting will be lost and you will NOT be able to get it back.') . '</strong>';
}
}
/**
* Return the default argument.
*/
function get_argument() {
return isset($this->argument->options[$this->option_name]) ? $this->argument->options[$this->option_name] : '';
}
}
/**
* @}
*/
?>As you see our plug-in is derived from the class views_plugin found in line 349 of plugins.inc, building upon the views_object, as defined from line 12 of base.inc onwards.
File and class names are completely at your discretion, the file will load automatically and be instantiated. Changing the method get_argument() in this class will quickly pass back ones own value.
Writing your own plug-in for Views should now be easy, just look at the example code.
Many thanks to dereinefor helping out!













