7

I am trying to add some custom view options. I am not even sure if what I'm trying to do is possible.

Basically I want to put a custom option under the Other category.

It would look like:

My custom options: disabled

When the user clicks on "disabled", it should show a custom form where the user can set some settings for that view.

I have looked into this, but couldn't find anything that would let me add custom options here.

enter image description here

  • What settings are you intending to add? – Patrick Kenny Jan 31 '13 at 22:42
  • what options are you wanting the user to be allowed to select? – Jimajamma Jan 31 '13 at 22:42
  • 1
    Hard to think of a Views option that isn't already there ;) – David Thomas Feb 1 '13 at 0:57
  • I am creating a new module. In simple terms, the option could just be a true/false value. The user will be able to enable/disable a feature that I am creating. I will create a post_execute() hook I think, to check if the value is true/false, and then act on that value. – donutdan4114 Feb 1 '13 at 2:33
1

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) {
      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.'),
    );
  }

}

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

The form ID of the view edit form is views_ui_edit_form

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'views_ui_edit_form')
  {
        $other = &$form['displays']['settings']['settings_content']['tab_content']['details']['columns']['third']['collapse']['other'];
        $other['custom']['#theme'] = 'views_ui_display_tab_setting';
        $other['custom']['#description'] = 'Custom';
        $other['custom']['#link'] = '<a href="' .  base_path() . '/node/1" title="Get information on how to theme this display" id="views-page-1-custom">Custom</a>';

  }

}

The form item for the OTHER section is

['displays']['settings']['settings_content']['tab_content']['details']['columns']['third']['collapse']['other']

so in the function this form element is taken into the $other variable as reference for ease of use.

This one simply adds a link to the node/1 you can edit it to do what you want. And the example does not take ajax into concern you can print out the $other variable to get an idea of what all details are given and how it all works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.