Using 'taxonomy: term ID' as an argument in one of my Drupal views and I'm having trouble getting it to work correctly.
On the views administration page it states, "if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name."
How does one go about converting the term name from a URL into a term ID?
you didn't show how do link to this views with agruments, if like this: http://site.com/views_page/taxonomy_name, then:
arg(1) - it's you term_name, get id via: $term = taxonomy_get_term_by_name(arg(1));
result is $term->tid
But if http://site.com/views_page/taxonomy_name - is aliased path to taxonomy terms, views should see directly tids in argument handling via arg(2) in taxonomy/term/TID path.
Related
Drupal 8
I have a content type with a field that holds a Taxonomy Term Reference. I want a View that shows other nodes of the same content type that do not have that field with the same Taxonomy Term ID as the currently viewed node.
Example:
URL: /story/storyA
Currently viewing a node of Content Type Story that has a Taxonomy Term Reference to value B. I need the view to show other stories that do not have that same Taxonomy Term Reference to value B.
I thought I could achieve that by
Set up a relationship to the field that holds the Taxonomy Term
Add a Filter Criteria of Term ID associated to above relationship and with operator is not equal to. PROBLEM: how do I get the token for Value?
I also tried to go about adding a Contextual Filter associated to the created relationship and selecting Exlude but no dice.
I finally figured it out, I was missing the adequate additional Contextual Filter:
Term ID , Provide default value with Taxonomy term ID from URL and the options Load default filter from node page and limit terms by vocab checked. And the last bit, in More, I have Exclude checked. This contextual filter is associated to the created relationship mentioned in the question.
So in Drupal 7 it was very easy to pass a taxonomy term by name as a contextual filter and it USED to have the option of convert term name to id. What's the equivalent of that in Drupal 8? I've tried the validator as "taxonomy term name" and it doesn't work. When I try to pass it by name I get nothing, but when I do ID it works. So I don't understand how to pass the actual taxonomy term name as the filter.
You first have to create a relation with the taxonomy terms by adding the relationship with the taxonomy list or Taxonomy terms on node. Once you have added the relation, you can now add a contextual filter called name with the category Taxonomy term. This way you can pass the taxonomy term by name to the filter.
I have two vocabularies ( Man and Woman ) with their regarding terms.
These terms are used as fields in content types Men and Women too. Now i wanna filter contents by content type and those vocabularies' terms by Views module ( custom view with name gallery ) in help of Contextual Filters as below :
http://mysite.com/gallery/content-type/(Man or Woman)'s terms
something like this :
1 - http://mysite.com/gallery/men/jean
2 - http://mysite.com/gallery/women/jean
1st url "jean" term is for vocabulary Man and the 2th url "jean" term is for vocabulary Woman.
I should add 3 Filters, one for content type one for terms of Woman and one for terms of Man. but the two Man and Woman Vocabularies should be filtered at one %.
I know by using this kind of path gallery/%/% the work should be done BUT filtering from two vocabularies' terms in the second % in url is my problem.
any help would be appreciated.
Views won't be able to distinguish between "jean" in url #1 and #2 if you use a taxonomy filter validating with "tid from term name".
The quick and dirty way to solve this is with two views: gallery/men/% and gallery/women/%.
They will be identical except in their term filter, which you would set to validate by the appropriate vocabulary. (They would also differ in their regular filter for content type.)
The programmatic way to solve this would involve using the first contextual filter as input to the second so you can limit by vocabulary. For example, something like
$vocabulary = $view->args[0]; // assuming this is the machine name
$term = taxonomy_get_term_by_name($view->args[1], $vocabulary);
return (!empty($term)) ? $term->tid : 0;
I've created content type called 'price' and defined field called 'weight'. Also I've created field 'Taxonomy term reference'. When clicking on Taxonomy term it shows all nodes related to this term. But How I can sort them by WEIGHT field?
At this moment I've created pages for all terms in views(I was lucky, I've got only 8 terms). But if terms would be more than 8. How can I automate this process? I would like create only one views for all terms.
Define Views for field reference -> node listing -> add relation for taxonomy term -> add sorting for this relation.
// Application
Drupal 7
// Problem Background
My website imports product data from a CSV file. I have a module that parses the file and creates appropriate nodes. In the CSV file, product category is given by category ID, which are different than Taxonomy Term IDs I have for the product categories in Drupal. I have created a custom field in my Taxonomy Vocabulary ("category_id") to link Drupal categories to imported category IDs.
When creating a node during import, I need "tid" (Taxonomy Term ID) to assign a node to a Taxonomy Term.
// The Question
How can I find Taxonomy Term ID ("tid"), knowing the value of a custom Taxonomy field?
Your custom taxonomy field ("category_id") creates a table field_data_field_category_id which should have an entity_id column/field which is your tid.
If you have command line Drush access, you could do:
drush php-eval '$tax=taxonomy_vocabulary_machine_name_load("main_site_structure");echo $tax->vid;'