hey there,

as the title says i want to create a view for a catalog handling multiple arguments using term names.
i really tried 100 different configurations using names. most times only the first arguiment gets recognized, other times the view is stuck in an infinite loop timing out.

i use the validation criteria "Term IDs separated by , or +" with the options allow multiple values.
however if i try to use "Term name converted to Term ID" as criteria i cant use the AND and OR operators anymore.

Any suggestions how to use the operations in conjunction with term names?

** Sidenote pulling in any taxonomy related relationsships didnt help :(((

here is my actual view using term ids, which by the way works like a charm:

$view = new view;
$view->name = 'taxonomy_view';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'taxonomy-view';
$view->core = 7;
$view->api_version = '3.0-alpha1';
$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['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['distinct'] = TRUE;
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'grid';
$handler->display->display_options['row_plugin'] = 'node';
$handler->display->display_options['row_options']['links'] = FALSE;
/* 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;
/* 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';
/* Contextual filter: Content: Has taxonomy term ID */
$handler->display->display_options['arguments']['tid']['id'] = 'tid';
$handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_index';
$handler->display->display_options['arguments']['tid']['field'] = 'tid';
$handler->display->display_options['arguments']['tid']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['tid']['default_argument_skip_url'] = 0;
$handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['arguments']['tid']['specify_validation'] = 1;
$handler->display->display_options['arguments']['tid']['validate']['type'] = 'taxonomy_term';
$handler->display->display_options['arguments']['tid']['validate_options']['vocabularies'] = array(
  'catalog' => 'catalog',
  'tags' => 0,
);
$handler->display->display_options['arguments']['tid']['validate_options']['type'] = 'tids';
$handler->display->display_options['arguments']['tid']['validate_options']['transform'] = 0;
$handler->display->display_options['arguments']['tid']['break_phrase'] = 1;
$handler->display->display_options['arguments']['tid']['add_table'] = 1;
$handler->display->display_options['arguments']['tid']['require_value'] = 0;
$handler->display->display_options['arguments']['tid']['reduce_duplicates'] = 0;
$handler->display->display_options['arguments']['tid']['set_breadcrumb'] = 0;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 0;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'product_display' => 'product_display',
);
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'taxonomy-view';
/* Display: Feed */
$handler = $view->new_display('feed', 'Feed', 'feed');
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['style_plugin'] = 'rss';
$handler->display->display_options['row_plugin'] = 'node_rss';
$handler->display->display_options['path'] = 'taxonomy-view.xml';
$handler->display->display_options['displays'] = array(
  'default' => 'default',
  'page' => 'page',
);
$translatables['taxonomy_view'] = array(
  t('Master'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Items per page'),
  t('- All -'),
  t('Offset'),
  t('All'),
  t('Page'),
  t('Feed'),
);

thanks in advance

fooby

Files: 
CommentFileSizeAuthor
#41 views-taxonomy_multiple_term_names-1248300-41.patch2.22 KByechuah
PASSED: [[SimpleTest]]: [MySQL] 1,658 pass(es).
[ View ]
#36 views-taxonomy_multiple_term_names-1248300-36.patch4.54 KBacrollet
PASSED: [[SimpleTest]]: [MySQL] 1,636 pass(es).
[ View ]
#34 1248300-34-views-taxonomy-multiple-term-names-filter.patch4.47 KBacrollet
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]
#33 1248300-33-views-taxonomy-multiple-term-names-filter.patch4.45 KBacrollet
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]
#31 1248300-30-views-taxonomy-multiple-term-names-filter.patch4.41 KBnaught101
PASSED: [[SimpleTest]]: [MySQL] 1,553 pass(es).
[ View ]
#30 1248300-29-views-taxonomy-multiple-term-names-filter.patch4.49 KBnaught101
PASSED: [[SimpleTest]]: [MySQL] 1,553 pass(es).
[ View ]
#23 1248300-22-views-taxonomy-multiple-terms-filter.patch4.54 KBcovenantd
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-22-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#15 1248300-15-views-taxonomy-multiple-terms-filter.patch4.32 KBcovenantd
Test request sent.
Previous result: FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-15-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#13 1248300-13-views-taxonomy-multiple-terms-filter.patch4.41 KBcovenantd
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-13-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#11 1248300-11-views-taxonomy-multiple-terms-filter.patch4.53 KBcovenantd
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-11-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#7 catalog_test_with_converted_name_2.txt13.86 KBfooby
#5 catalog_test_with_converted_name.txt13.85 KBfooby
#5 names_0.1.patch4.54 KBfooby
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in names_0.1.patch.
[ View ]
#2 term_without_relation.txt5.94 KBfooby
#1 term_with_relation.txt6.44 KBfooby

Comments

StatusFileSize
new6.44 KB

i attached a different approach using a relationship to the taxonomy.
the contextual filter (Has taxonomy term ID) is linked to the relationship and validation is using "Term name converted to Term ID", also i enabled "Allow multiple filter values to work together".
As described in my earlier post everything after the first argument gets ignored.

To specify my needs:

the catalog should be able to filter by multiple terms names using contextual filters.
for example:

product1 has taxonomy 1 2
product2 has taxonomy 1 3
product3 has taxonomy 1 2 3
product4 has taxonomy 1 4

example.com/taxonomy-view/1/4 should only show product 4
example.com/taxonomy-view/2/4 should only show nothing
example.com/taxonomy-view/1/2 should only show product 1 and 3
example.com/taxonomy-view/1/3 should only show product 1,2 and 3

wheter this is with "/" using multiple arguments or operators "+" and "," using only 1 argument doesnt matter.
the important thing is to make it work with term names

StatusFileSize
new5.94 KB

here my first view as attachment

So you are right this is not possible at the moment without custom code.
If you want you could extend modules/taxonomy/views_plugin_argument_validate_taxonomy_term.inc to support this.

unfortunately im not that into module development yet.
would it be difficult to implement this?

StatusFileSize
new4.54 KB
FAILED: [[SimpleTest]]: [MySQL] Invalid patch format in names_0.1.patch.
[ View ]
new13.85 KB

i created a patch file and a corresponding view

names are now converted and checked properly, but the AND operator doesnt work.

i could use a review. this patch is for 3.0-rc1

note: there are still some prints while its not finished

Category:support» feature
Status:Active» Needs review

Update status

Category:feature» support
Status:Needs review» Active
StatusFileSize
new13.86 KB

catalog_test_with_converted_name.txt was not reusable

adding updated version

Status:Active» Needs review

Status:Needs review» Needs work

The patch is still full of dev comments and includes a <pre>. Apart from that, it doesn't work as expected. Entering just one element returns no results, and the operator always seems to be "and", whether "," or "+" is provided.

There's also an issue with this patch for terms that contain spaces because of views_break_phrase_string().

item 1,item 2 gets rendered correctly into:
[0] => item 1
[1] => item 2

item 1+item 2 however gets rendered into:
[0] => item
[1] => 1
[2] => item
[3] => 2

This causes incorrect results. The views option to turn spaces into dashes doesn't seem to help for this issue.

Status:Needs work» Needs review
StatusFileSize
new4.53 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-11-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Wrote a patch for this that is working well for me. Decided to start from scratch after reading comments 9 and 10, plus fooby's comment 4 (admirable that you were still willing to take a stab at a patch, fooby!)

Some notes and known issues that I need direction on:

  1. Per comment 10, the views_break_phrase_string() issue - it looks like there is close to a working patch in #1027458: Allow to use multiple arguments with spaces so will probably just wait for that to be finished as it is the proper place to handle spaces
  2. My Drupal $query knowledge is lacking, so wasn't sure how to convert the $query->where("replace(td.name, ' ', '-') = :name", array(':name' => $convert)); line into something that works with IN, so for now this won't work if you have the Transform spaces into URLs option turned on
  3. I found usages of function process_summary_arguments(&$args) in theme\theme.inc, but can't figure out where those would be rendered or what it really does or how to see/test it? I just added in 'convertterms' to run through the same code that was already there for 'convert', but probably not a good idea since I don't know what it does!
  4. Final question is - since I basically took the Term IDs separated by , or + and Term name converted to Term ID options and mushed them together, it is running two queries for effectively the same data. I think we can drop the second and just use the data picked up in the first, however I wasn't sure if that would skip/hurt/mess-up the little $validated_cache section?

Status:Needs review» Needs work

The last submitted patch, 1248300-11-views-taxonomy-multiple-terms-filter.patch, failed testing.

Status:Needs work» Needs review
StatusFileSize
new4.41 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-13-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Changing directories from /sites/all/modules/contrib to just /sites/all/modules in hopes of making the testbot happy.

Status:Needs review» Needs work

The last submitted patch, 1248300-13-views-taxonomy-multiple-terms-filter.patch, failed testing.

StatusFileSize
new4.32 KB
Test request sent.
Previous result: FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-15-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Ah, apparently needs to be relative to the views directory - better luck this time I'm hoping? Sorry for the thread spam, learning here.

Status:Needs work» Needs review

Status:Needs review» Needs work

The last submitted patch, 1248300-15-views-taxonomy-multiple-terms-filter.patch, failed testing.

Version:7.x-3.0-rc1» 7.x-3.3
Status:Needs work» Needs review

Alright, apparently I need some help on getting this patch to apply too! Ah, maybe the Views version as I'm working with 7.x-3.3. Come to think of it, probably need to re-roll this against dev, but going to first see if this will do the trick.

Re: views_break_phrase_string() - I tried applying the patch from comment 10 in #1027458: Allow to use multiple arguments with spaces but it didn't do the trick. Seems to come down to this in \includes\handlers.inc:

<?php
   
// The '+' character in a query string may be parsed as ' '.
   
$handler->operator = 'or';
   
$handler->value = explode(' ', $str);
?>

I changed this to:

<?php
   
// The '+' character in a query string may be parsed as ' '.
   
$handler->operator = 'or';
    if(!empty(
$handler->options['use_plus_separator'])) {
     
$handler->value = explode('+', $str);
    } else {
     
$handler->value = explode(' ', $str);
    }
?>

Then I added that in when it called through like this:

<?php
        $terms
= new stdClass();
       
$terms->value = $argument;
       
$terms->options['use_plus_separator'] = TRUE;
       
$terms = views_break_phrase_string($argument, $terms);
?>

Did the trick, but not sure it is the best way?

Found a bug in my patch today, need to check if any terms returned after doing the first query. Will add to new patch file, but need help figuring out why it is failing testing. In the meantime:

<?php
       
// No terms is not valid so return
       
if (count($test) == 0) {
          return
FALSE;
        }
?>

Version:7.x-3.3» 7.x-3.x-dev
Category:support» feature
Status:Needs review» Needs work

Please reroll the patch against the dev version and reupload it. It will only pass tests against the dev.

StatusFileSize
new4.54 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch 1248300-22-views-taxonomy-multiple-terms-filter.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Here we are against dev - fourth try lucky?

Status:Needs work» Needs review

Status:Needs review» Needs work

The last submitted patch, 1248300-22-views-taxonomy-multiple-terms-filter.patch, failed testing.

The patch looks pretty good and worked for me. One comment, there's a few lines that are commented out:

+        if ($transform) {
+          // TODO - change from WHERE to use IN in a Drupal-ized way
+          //$query->where("replace(td.name, ' ', '-') = :name", array(':name' => $convert));
+        }

Otherwise this looks great and worked for the cases I tested with.

Thanks for testing - yeah, the commented out lines are what I'm needing some guidance on (see comment 11, item 2 above).

Title:how to create a view handling multiple arguments on taxonomy using term namesContextual filters: Taxonomy filter validation using multiple term names

I think the patch isn't applying with the test bot because it's using windows-style formatting for the file name (the directories are separated by "\\" instead of "/"). I replaced that and the patch applied ok. Also, watch out for excess whitespace.

StatusFileSize
new4.49 KB
PASSED: [[SimpleTest]]: [MySQL] 1,553 pass(es).
[ View ]

Little hacking. Still needs work. This version treats both "," and "+" as "OR". Not sure where that is dealt with yet.

Status:Needs work» Needs review
StatusFileSize
new4.41 KB
PASSED: [[SimpleTest]]: [MySQL] 1,553 pass(es).
[ View ]

Um.. so as far as I can work out, there is no AND in views' taxonomy filters:

views_handler_argument_term_node_tid_depth.inc:

<?php
36     $form
['break_phrase'] = array(
37       '#type' => 'checkbox',
38       '#title' => t('Allow multiple values'),
39       '#description' => t('If selected, users can enter multiple values in the form of 1+2+3. Due to the number of JOINs it would require,     AND will be treated as OR with this filter.'),
?>

Go figure. I guess this is working for me, aside from that one major problem, which probably should be dealt with as a separate bug.

StatusFileSize
new4.45 KB
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]

This patch is working well for me, except the logic was inverted when converting hyphens to spaces in term names. Patch attached that corrects that issue and cleans up the comments. I otherwise consider this code RTBC.

StatusFileSize
new4.47 KB
PASSED: [[SimpleTest]]: [MySQL] 1,603 pass(es).
[ View ]

Looks like I spoke slightly too soon, this version declares $test as an array before adding elements to it to eliminate a php notice.

I can confirm that the patch at #34 works as designed. One caveat is that you cannot use placeholders in the path setup for full page views. For example, products/%/%/% as the Path under Page Settings causes a few PHP warnings and a broken view. Removing the placeholders from that setting, i.e. just products allowed a three-vocab view with multiple term names for each contextual filter to work as expected. Further, using %1, %2, and %3 still worked in breadcrumb and title overrides, too.

Nice work @acrollet, ye really saved a show stopper for me.

StatusFileSize
new4.54 KB
PASSED: [[SimpleTest]]: [MySQL] 1,636 pass(es).
[ View ]

slightly improved version of the patch attached.

Status:Needs review» Reviewed & tested by the community

Code looks good, works for me, with the caveat in #32.

My problem is a bit different from the issue here, which "term name converted to term id" returns only one tid even if there are several tids having the same name (for example, in different levels).

For this problem, #36 also works for me.

Maybe, similar patch is required for "term name converted to term id" to make it compatible with multiple tids of one word.

It is great that there are some manual tests!

+++ b/modules/taxonomy/views_plugin_argument_validate_taxonomy_term.incundefined
@@ -189,6 +190,97 @@ class views_plugin_argument_validate_taxonomy_term extends views_plugin_argument
+        $query = db_select('taxonomy_term_data', 'td');
+        $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
+        $query->fields('td');
+        $query->fields('tv', array('machine_name'));
+        if (!empty($vocabularies)) {
+          $query->condition('tv.machine_name', $vocabularies);
+        }
...
+          $query = db_select('taxonomy_term_data', 'td');
+          $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
+          $query->fields('td');
+          $query->fields('tv', array('machine_name'));
+          $query->condition('td.tid', $test);

Won't it be possible to replace these two queries with an entity field query?

StatusFileSize
new2.22 KB
PASSED: [[SimpleTest]]: [MySQL] 1,658 pass(es).
[ View ]

Couldn't get #36 to work. Created a rough patch for my needs.

Hi,
what is the solution/patch for D6 (View 6.x-3.0-rc3)?

Thanks,
WaPoNe

@WaPoNe, there is no D6 solution at this time. It may be possible to backport once a D7 solution has been settled on.

@yechuah, please don't upload a new patch without setting the issue to "needs review". Also, if you could give us more information about what didn't work about #36 that would be helpful. #36 is working fine for me.

In addition, #41 has several problems including not recognizing the transform option (convert spaces to -) and uses regex's instead of views_break_phrase_string().

We are running into a problem with this - I've tried #36 and #41 (best results were with #41, #36 only returned a query for the first term name) -has anyone else run into this?

Content managers are supplying a comma separated list of taxonomy terms (apple,orange,banana) for a contextual filter (using Insert View).

We had tested prior to to content being added en masse, but now we are seeing results that are different from what we expected (and most certainly from what the results when we tested in November)

If the manager specifies apple - the view outputs nodes tagged with apple. If the manager specifies orange - the view outputs nodes tagged with orange. If the manager specifies apple,orange (or apple+orange) - the view outputs the nodes tagged with apple If the manager specified orange, apple - the view outputs the nodes tagged with orange.

The query ends up including

WHERE (( (field_data_field_something.field_something_tid = '3903+3904' ) )AND(( (node.status = '1') AND (node.type IN ('content_type')) )))

What we are expecting to see is:

If the manager specifies apple,orange - the view outputs all nodes that are tagged with apple OR orange.

Anyone have any ideas on what might we might try to fix this or on what might have updated between Nov and March that would have changed the behavior on this? We tested apples,oranges and verified that we got posts from both apples and oranges in the results.....