1

I am setting up a contextual filter for several taxonomy fields on a content type that stores my organizations records. I want to present my users with a nice looking url, not the TID. For the contextual filter, I have selected Provide a Default Value and selected Use PHP Code. This is the code:

$term_name = "Open";
$termObj = taxonomy_get_term_by_name($term_name);
$term = reset($termObj);
return $term->tid;

For now, I'm hard coding the term until I can get it working, then I'll switch it to a term out of the URL. When I save the contextual filter, the preview shows "No query was run". After I save the view and visit the page created by the view, Drupal gives me a Page Not Found message.

  • What is the name of the contextual filter you're using? – David Thomas Apr 14 '13 at 23:26
  • The filter for the example above is for a field called age group (field_age_group), which is one of the fields I'm displaying in my view. – Robbert Apr 14 '13 at 23:42
  • Can you provide more info about field_age_group field type etc.. – David Thomas Apr 15 '13 at 3:57
  • The field is a simple taxonomy field with values of Open, "11 - 12", "13 - 14", etc. The content type is using a radio button to select the specific value. The view for which I'm trying to set up the contextual filter, displays the field just fine. Now, I'm just trying to figure out how to make a contextual filter for the field. I want to be able to use a nice looking contextual filter, like "open" or 1112 instead of passing the TID in the url. – Robbert Apr 15 '13 at 15:35
  • No idea why, but this started working today. – Robbert Apr 15 '13 at 21:14
3

As I am always against adding PHP code in the UI may I suggest a different approach?

Add a relationship to your view "Content: age group", which is the relationship from your term reference field to the taxonomy term.

Then add a contextual filter "Taxonomy term: Name", using the relationship you just added.

Then the argument will be the term name without you having to add custom PHP code.

  • Really nice solution! +1! – Ponzio Pilato Mar 1 '18 at 18:32

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.