Exposed forms as plugins

dagmar - July 30, 2009 - 18:52
Project:Views
Version:7.x-3.x-dev
Component:exposed filters
Category:task
Priority:normal
Assigned:Unassigned
Status:needs work
Issue tags:views 3.x roadmap
Description

Views 3 Roadmap doesn't say anything about implement exposed forms as plugins.

However I think that if headers, or footers will be plugins, exposed filters should be too. This will allow other modules to define especial exposed filters.

I'm marking this issue as 'need works' beacause it ins't ready. Comments are welcome.

Exposed forms as plugin may help to implement this features

From views 3 roadmap:

Ability to have view not run when no value selected
People constantly ask for this feature, wherein the view will be empty until something has been selected in the exposed filters.

Control of text on 'button'
Paired with a proper translation plugin, this would be a nice little touch.

Own section in UI
To start with, the exposed forms have enough possible settings that they need their own section in the UI so that we can give them their settings. We can intelligently unset this if nothing is using the exposed form or maybe we can require it to be turned on before the 'expose' button shows up on anything.

AttachmentSize
views-exposed-form-plugin.patch14.66 KB

#1

dagmar - July 31, 2009 - 15:54
Title:Exposed forms as plugin» Exposed forms as plugins
Status:needs work» needs review

More code.

This patch allows user to select between "Basic" exposed form and "On demand" exposed form.

The first is the standard exposed form, the second one only display the list if user set a value for at least one of the exposed filters available.

Changing status.

AttachmentSize
views-exposed-form-plugin-1.patch 18.6 KB

#2

dagmar - August 18, 2009 - 16:02

Some small fix:

  • Allow translate "Apply" button.
  • Check if exposed filter is enabled.

I was thinking that maybe this plugin is a possible way to implement: #228510: Exposed Sorts. With this patch, exposed forms can alter the query by using query()

AttachmentSize
views-exposed-form-plugin-2.patch 18.71 KB

#3

dereine - August 19, 2009 - 20:18

+++ views.module 18 Aug 2009 15:54:41 -0000
@@ -972,7 +972,7 @@ function views_exposed_form(&$form_state
-    '#value' => t('Apply'),
+    '#value' => t($form_state['submit_button']),

Why not let this be handled by the plugin. There could be perhaps a dynamic submit button, but there shouldn't be this string in the locale_source table.

+++ plugins/views_plugin_display.inc 18 Aug 2009 15:54:52 -0000
@@ -1547,6 +1636,29 @@ class views_plugin_display extends views
+          $plugin->options_submit($form['exposed_form_options'], $form_state);
+          krumo($form_state['values'][$section]);
+++ plugins/views_plugin_exposed_form.inc 18 Aug 2009 15:54:52 -0000
@@ -0,0 +1,96 @@
+  function option_defaults(&$options) {
+    $options['submit_button'] = t('Apply');

needs to be removed before commiting the patch.

+++ plugins/views_plugin_exposed_form.inc 18 Aug 2009 15:54:52 -0000
@@ -0,0 +1,96 @@
+  function option_defaults(&$options) {
+    $options['submit_button'] = t('Apply');

So currently the submit button value is handled full by the plugin. This would cause currently apply be translated to "foo". And then t("foo") will be called again.

This review is powered by Dreditor.

#4

dagmar - August 19, 2009 - 21:42

Thanks dereine:

New patch.

BTW. I'm not sure if this is the better way to obtain an empty text for a view.

<?php
 
function query() {
    if (!
$this->exposed_filter_applied()) {
     
// clear WHERE statment to display empty text
     
$this->view->query->where = array();
     
$this->view->query->add_where(0, '1 = 0');
    }
?>

What do you think?

AttachmentSize
views-exposed-form-plugin-3.patch 18.7 KB

#5

dereine - August 23, 2009 - 09:55

Just tagging to views 3.x

#6

merlinofchaos - November 2, 2009 - 22:42

Despite a couple of small problems, I'm tempted to go ahead and commit this. This is something I like a lot. Great work!

1) There's a leftover krumo() statement.
2) The option_defaults() is outdated and needs to be pulled from everywhere. Go with the option_definition() pattern used in styles and row styles instead. That is more robust.
3) Let's give the "Exposed" options their own category in the UI. I plan to re-org this stuff a little anyhow.

The fact that we need option_definition as opposed to option_defaults is the only thing preventing me from just committing it now; that's going to be a pain to go back and fix. So let's fix that, then I can commit it.

#7

dagmar - November 3, 2009 - 03:27

I have rebuilt it fixing the items that you have remarked.

Also this patch fixes an small bug in #4, now it is possible define different input formats for "Text On Demand" and "Empty Text"

BTW, I doesn't speak english very well, I'm not sure if "On Demand" is the better name for one of the plugins. Maybe there is another better. Please feel free to change it if know another better.

AttachmentSize
views-pluggable-exposed-forms.patch 18.81 KB

#8

dagmar - November 7, 2009 - 14:37

Please, see #228510: Exposed Sorts. I have created a patch that includes this patch and the support for exposed sorts.

#9

merlinofchaos - November 11, 2009 - 01:09
Version:6.x-3.x-dev» 7.x-3.x-dev
Status:needs review» patch (to be ported)

Cleaned up a bit and committed. Patch attached because this needs to be ported.

AttachmentSize
exposed-form-plugin.patch 13.18 KB

#10

merlinofchaos - November 11, 2009 - 01:47

I think that patch is missing the plugin class files; they'll have to be pulled from the 6.x-3.x branch.

#11

dereine - November 11, 2009 - 06:50

Here is the first part. The settings of the exposed forms, are not displayed currently.

The plugins are included in the patch.

AttachmentSize
views-exposed-d7.patch 18.11 KB

#12

dagmar - November 11, 2009 - 14:13

+++ plugins/views_plugin_exposed_form_on_demand.inc 11 Nov 2009 06:49:14 -0000
@@ -0,0 +1,71 @@
+      $this->view->query->add_where(0, '1 = 0');

I think that this patch is not based on last commit. This line is different in the repository.

#13

dereine - November 13, 2009 - 13:46
Status:patch (to be ported)» needs work

@Dagmar:
This is part of the current drupal6 _on_demand exposed form plugin. I don't do anything here.

New version:

- Current problem:
Exposed_input is sometimes returns as array() sometimes as $value.

Here is a example view to reproduce this.

$view = new view;
$view->name = 'frontpage';
$view->description = 'Emulates the default Drupal front page; you may set the default home page path to this view to make it your front page.';
$view->tag = 'default';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('sorts', array(
  'sticky' => array(
    'id' => 'sticky',
    'table' => 'node',
    'field' => 'sticky',
    'order' => 'DESC',
  ),
  'created' => array(
    'id' => 'created',
    'table' => 'node',
    'field' => 'created',
    'order' => 'DESC',
    'relationship' => 'none',
    'granularity' => 'second',
  ),
));
$handler->override_option('filters', array(
  'promote' => array(
    'operator' => '=',
    'value' => '1',
    'group' => 0,
    'exposed' => TRUE,
    'expose' => array(
      'use_operator' => FALSE,
      'operator' => '',
      'identifier' => 'promote',
      'label' => 'Promoted to front page',
      'remember' => FALSE,
      'single' => TRUE,
      'optional' => FALSE,
    ),
    'id' => 'promote',
    'table' => 'node',
    'field' => 'promote',
  ),
  'status' => array(
    'operator' => '=',
    'value' => '1',
    'group' => 0,
    'exposed' => TRUE,
    'expose' => array(
      'use_operator' => FALSE,
      'operator' => '',
      'identifier' => 'status',
      'label' => 'Published',
      'remember' => FALSE,
      'single' => TRUE,
      'optional' => FALSE,
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
  'role' => array(),
  'perm' => '',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('use_pager', '1');
$handler->override_option('row_plugin', 'node');
$handler->override_option('row_options', array(
  'teaser' => 1,
  'links' => 1,
));
$handler->override_option('exposed_form', array(
  'type' => 'basic',
  'options' => array(),
));
$handler = $view->new_display('page', 'Page', 'page');
$handler->override_option('path', 'frontpage');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
));
$handler = $view->new_display('feed', 'Feed', 'feed');
$handler->override_option('title', 'Front page feed');
$handler->override_option('style_plugin', 'rss');
$handler->override_option('style_options', array(
  'mission_description' => 1,
  'description' => '',
));
$handler->override_option('row_plugin', 'node_rss');
$handler->override_option('row_options', array(
  'item_length' => 'default',
));
$handler->override_option('path', 'rss.xml');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
));
$handler->override_option('displays', array(
  'default' => 'default',
  'page' => 'page',
));
$handler->override_option('sitename_title', '1');

AttachmentSize
exposed-form-plugin-7.patch 21.57 KB

#14

dagmar - November 13, 2009 - 14:45

@dereine:

Cleaned up a bit and committed.

Earl Miles changed the name and some parts of these plugins. Please see http://drupal.org/cvs?commit=287352

#15

dereine - November 13, 2009 - 17:33

Ah ok. thanks for this information.

#16

merlinofchaos - November 16, 2009 - 06:17

Sorry, dagmar admitted he wasn't sure On Demand was right, so I changed it as I worked on it. I also really dislike adding WHERE clauses to the query to force it to produce an invalid result, and instead made sure that there is a way to abort the query by setting a value in the build info.

 
 

Drupal is a registered trademark of Dries Buytaert.