Skip to main content
Fixed problem in options_form function
Source Link
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
      parent::options_form($form, $form_state);
      
      if ($form_state['section'] === 'mymodule') {
        $form['my_setting'] = array(
          '#type' => 'checkbox',
          '#title' => t('Some Setting'),
          '#description' => t('A sample checkbox.'),
          '#default_value' => $this->display->get_option('my_setting'),
        );
      }
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
      $form['my_setting'] = array(
        '#type' => 'checkbox',
        '#title' => t('Some Setting'),
        '#description' => t('A sample checkbox.'),
        '#default_value' => $this->display->get_option('my_setting'),
      );
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
      parent::options_form($form, $form_state);
      
      if ($form_state['section'] === 'mymodule') {
        $form['my_setting'] = array(
          '#type' => 'checkbox',
          '#title' => t('Some Setting'),
          '#description' => t('A sample checkbox.'),
          '#default_value' => $this->display->get_option('my_setting'),
        );
      }
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
Removed an unnecessary check in options_summary
Source Link
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
    if ($form['#section'] == 'page-mymodule') {
      $form['my_setting'] = array(
        '#type' => 'checkbox',
        '#title' => t('Some Setting'),
        '#description' => t('A sample checkbox.'),
        '#default_value' => $this->display->get_option('my_setting'),
      );
    }
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
    if ($form['#section'] == 'page-mymodule') {
      $form['my_setting'] = array(
        '#type' => 'checkbox',
        '#title' => t('Some Setting'),
        '#description' => t('A sample checkbox.'),
        '#default_value' => $this->display->get_option('my_setting'),
      );
    }
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
      $form['my_setting'] = array(
        '#type' => 'checkbox',
        '#title' => t('Some Setting'),
        '#description' => t('A sample checkbox.'),
        '#default_value' => $this->display->get_option('my_setting'),
      );
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}
Source Link

What you're actually looking to do is to implement a custom Views Display Extender plugin (https://api.drupal.org/api/views/views.api.php/group/views_plugins/7.x-3.x).

It's not particularly trivial and I got most of my information from this wonderful tutorial here: How to write a Views Plugin Display Extender (D7) by Bryan Ollendyke

Here's a basic outline:

-create a custom module (let's call it mymodule) and implement hook_views_api().

/**
 * Implements hook_views_api().
 */
function mymodule_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'mymodule'),
  );
}

-Create a mymodule.views.inc file (remembering to add the file to your mymodule.info file) and implement hook_views_plugins(). It should look something like this:

/**
* Implements hook_views_plugins().
 */
function mymodule_views_plugins() {
  $path = drupal_get_path('module', 'my_module');
  $plugins = array();
  
  $plugins['display_extender'] = array(
    'mymodule' => array(
      'title' => t('Some Setting'),
      'help' => t('A description of the setting.'),
      'path' => $path,
      'handler' => 'mymodule_views_plugin_display_extender',
    ),
  );
  
  return $plugins;
}

-Note the "handler" index in the above array. You'll need to implement that in a file called mymodule_views_plugin_display_extender.inc (again, remembering to add that in your mymodule.info file). The implementation should look something like this:

class mymodule_views_plugin_display_extender extends views_plugin_display_extender {
  /**
   * Provide a form to edit options for this plugin.
   */
  function options_definition_alter(&$options) {
    $options['my_setting'] = array(
      'default' => 0,
    );
  }

  /**
   * Provide a form to edit options for this plugin.
   */
  function options_form(&$form, &$form_state) {
    if ($form['#section'] == 'page-mymodule') {
      $form['my_setting'] = array(
        '#type' => 'checkbox',
        '#title' => t('Some Setting'),
        '#description' => t('A sample checkbox.'),
        '#default_value' => $this->display->get_option('my_setting'),
      );
    }
  }
  
  /**
   * Handle any special handling on the validate form.
   */
  function options_submit(&$form, &$form_state) {
    $this->display->set_option('my_setting', $form_state['values']['my_setting']);
  }

  /**
   * Provide the default summary for options in the views UI.
   *
   * This output is returned as an array.
   */
  function options_summary(&$categories, &$options) {
    $options['mymodule'] = array(
      'category' => 'other',
      'title' => t('Some Setting'),
      'value' => ($this->display->get_option('my_setting')) ? 'Yes' : 'No',
      'desc' => t('Set a setting.'),
    );
  }
  
}

After all that your mymodule.info file should look something like this:

name = My Module
description = Sample views display extender
core = 7.x
version = 7.x-1.0

dependencies[] = views

files[] = mymodule.views.inc
files[] = mymodule_views_plugin_display_extender.inc

If that all works out, you should see a setting show up under "Other" (you can change that using the 'category' in the options_summary implementation in mymodule_views_plugin_display_extender.inc.

After that, in your module, you can access the option from the view using something like:

$view->display_handler->get_option('my_setting');

For instance, if you were implementing `hook_views_post_execute().

/**
 * Implements hook_views_post_execute().
 */
function mymodule_views_post_execute(&$view) {
  $my_setting = $view->display_handler->get_option('my_setting');
}