Extra spaces at the start or end of text entered into an exposed filter can cause problems. For example, if you have something with "foo" and you filter for "foo" you'll get the result. But, if you filter for "foo " (note the extra space) you can get into a problem.

Either by default or via an option this should be passed through trim().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

neoglez’s picture

Well yes, the question here is where to introduce the trim: in this class (wich method?) or in the exposed form plugin...
I think one should try to make it as more general as possible.
here an export to clarify:

$view = new view;
$view->name = 'issue_1301746_extra_spaces';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Issue 1301746 Extra spaces';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Issue 1301746 Extra spaces';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['style_options']['group_rendered'] = 1;
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['hide_empty'] = 0;
$handler->display->display_options['row_options']['default_field_elements'] = 1;
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/* Field: Content: Body */
$handler->display->display_options['fields']['body']['id'] = 'body';
$handler->display->display_options['fields']['body']['table'] = 'field_data_body';
$handler->display->display_options['fields']['body']['field'] = 'body';
$handler->display->display_options['fields']['body']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['body']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['body']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['body']['alter']['external'] = 0;
$handler->display->display_options['fields']['body']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['body']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['body']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['body']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['body']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['body']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['body']['alter']['trim'] = 0;
$handler->display->display_options['fields']['body']['alter']['html'] = 0;
$handler->display->display_options['fields']['body']['element_label_colon'] = 1;
$handler->display->display_options['fields']['body']['element_default_classes'] = 1;
$handler->display->display_options['fields']['body']['hide_empty'] = 0;
$handler->display->display_options['fields']['body']['empty_zero'] = 0;
$handler->display->display_options['fields']['body']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['body']['type'] = 'text_plain';
$handler->display->display_options['fields']['body']['field_api_classes'] = 0;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'node';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'Title';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'title';
$handler->display->display_options['filters']['title']['expose']['required'] = 0;
$handler->display->display_options['filters']['title']['expose']['multiple'] = FALSE;

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'issue-1301746-extra-spaces';
$translatables['issue_1301746_extra_spaces'] = array(
  t('Master'),
  t('Issue 1301746 Extra spaces'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Body'),
  t('Title'),
  t('Page'),
);

dawehner’s picture

I think placing it both in views_handler_fitler::init and views_handler_filter::acceppt_exposed_input would be one solution

Another solution would be in pre_query()

dagmar’s picture

Status: Active » Needs review
FileSize
2.41 KB

Initial patch.

dawehner’s picture

Status: Needs review » Needs work
+++ b/handlers/views_handler_filter.incundefined
@@ -583,6 +583,10 @@ class views_handler_filter extends views_handler {
+      if (!empty($this->options['expose']['trim_spaces']) && !is_array($value)) {
+        $value = trim($value);

What about using array_map for arrays?

+++ b/handlers/views_handler_filter_string.incundefined
@@ -19,6 +19,7 @@ class views_handler_filter_string extends views_handler_filter {
+    $options['expose']['contains']['trim_spaces'] = array('default' => FALSE, 'bool' => TRUE);

I totally agree that this should be the default behavior, because this improves the UX.

DuaelFr’s picture

Status: Needs work » Needs review
FileSize
1.43 KB
2.08 KB

It looks good to me.
I integrated Daniel's advices.

davemaxg’s picture

I'm interested in using this functionality on a production site running 7.x-3.7. Is the safest approach to apply this patch to 3.7 or just install the dev branch? I'm assuming that the next release will incorporate this feature, correct?

Thanks,

David

pjcdawkins’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #5 works.

It's an excellent and simple UX improvement.

@davemaxg: Patches are usually designed to apply to -dev, but in this case the patch works against both 7.x-3.7 and the current latest 7.x-3.x-dev.

pjcdawkins’s picture

SukanyaDhan’s picture

Issue summary: View changes

Hi,
The patch #5 worked perfectly fine.
I would like to know if the trim feature comes by default in the Views version greater than 2.16 for Drupal 6?

Murz’s picture

Thanks, patch works very well, will be good to see it in views core.

colan’s picture

We've recently switched our testing from the old qa.drupal.org to DrupalCI. Because of a bug in the new system, #2623840: Views (D7) patches not being tested, older patches must be re-uploaded. On re-uploading the patch, please set the status to "Needs Review" so that the test bot will add it to its queue.

If all tests pass, change the Status back to "Reviewed & tested by the community". We'll most likely commit the patch immediately without having to go through another round of peer review.

We apologize for the trouble, and appreciate your patience.

colan’s picture

Status: Reviewed & tested by the community » Needs work
RdeBoer’s picture

As this patch was never added to Views and because I don't like the maintenance overhead of patched modules on my system, I wrote a mini-module that implements the functionality of the patch of #5.
It is available here: https://www.drupal.org/sandbox/rdeboer/2660818

pjcdawkins’s picture

Status: Needs work » Needs review
FileSize
2.08 KB

Re-uploading the patch from #5

edit: note that the patch is by DuaelFr

DuaelFr’s picture

Thanks for reposting.
Can we reproduce in Drupal 8 ? If so, we should move this issue in the Core queue so it can be fixed using the common workflow (edge version then backport).

TommyK’s picture

I just ran into the white space issue. I had to re-roll the patch to get it to apply to the current 7.x-3.x-dev. I did not change any of the functional content, but the line numbers significantly changed.

Status: Needs review » Needs work
biguzis’s picture

biguzis’s picture

Status: Needs work » Needs review
thetwentyseven’s picture

DamienMcKenna’s picture

@thetwentyseven: Hello there, thanks for trying to put together that patch. However, it seems you didn't create the patch correctly as the paths start with "sites/all/modules", you might want to reread the docs on creating a patch to see if there's a step you missed. Also, what was the intention of the patch - were you rerolling it, were there intended changes on the patch itself, etc? Thanks.

AlbionBrown’s picture

Updated patch file for 7.x-3.23

AlbionBrown’s picture

That's annoying, the patch applied fine for me. Does it apply fine manually for anyone else?

DamienMcKenna’s picture

The paths are incorrect for the patch, they should be relative to the module directory, not the site.

I've rerolled the patch, please give this one a try.

AlbionBrown’s picture

Ah! I hadn't spotted that, thanks!