Friendly URL for Views Contextual Filter on a Boolean Field
I have a story content type that has a corresponding Views listing page. The stories can be flagged as Featured through a checkbox/boolean field ( field_story_featured ). The stories also have a Story Type value ( field_story_type ). The views listing page needs to have the following links at the top:
The URL structure for the Views page makes the most sense as follows (purple = story type contextual filter, orange = featured contextual filter):
- All => /stories or /stories/all
- Featured => /stories/all/featured
- Students => /stories/students
- Alumni => /stories/alumni
- Faculty => /stories/faculty
Notice that I want to use the word “featured” in the URL. Unfortunately this doesn’t work out of the box in Drupal. The value for
field_story_featured is actually either a 0 or a 1. It doesn’t look very nice to have a url like
/stories/all/1 but we can remedy that with a little PHP in the field’s contextual filter:
PHP validate code:
1 2 3 4 |
if (empty($argument) || strtolower($argument) == 'featured') { $handler->argument = 1; return TRUE; } |
One Comment
MP
Hey that helped me ! Thx ! My case was pretty different but you got me going.
I’m doing a validation against a field on my taxonomy terms based on the url.
if (false !== $key = array_search($argument, array_column($term_data, ‘url’))){
$handler->argument = $term_data[$key][‘id’];
return TRUE;
}else{
return FALSE;
}