Development

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:

Screenshot_2017-02-08_15-05-57

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 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:

Screenshot_2017-02-08_15-22-05

Screenshot_2017-02-08_15-21-12

PHP validate code:

 

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;
    }

Leave a Reply

Your email address will not be published.