1

Is it possible to force Views to always lowercase URL paths? Here is a detailed example. I'm using contextual filters to filter by node field (field's name is country). Here are the node examples:

Node title: Madrid | country: spain
Node title: Rome | country: italy

Country name values are intentionally lowercased. Now I create a country contextual filter with this path: results/%. These are the results:

Path: results/spain
Result: Madrid

Path: results/Spain
Result: Madrid

Path: Results/Spain
Result: Madrid

I want the results to be available only on these paths:

results/spain

results/italy

Is there a way to force Views to convert all of the paths to lowercase letters? If it's not possible in Views, is there a way to make Drupal convert all of the URL requests to lowercase paths?

3

This is untested, but I believe you can use a hook_url_outbound_alter() for this, since I think Views URLs still get run through url() when they get generated:

function MYMODULE_url_outbound_alter(&$path, &$options, $original_path) {
  $path = drupal_strtolower($path);
}

(Updates to address some comments post acceptance.)

This should update URLs that get generated by Views, and will prevent the uppercased ones from getting out into the wild.

Drupal 7 is case-insensitive with regards to inbound URL routing. However, I am not 100% sure this is always the case with Views arguments being pulled from the URL. I suspect that the base part of the path w/o the argument is being matched by Drupal as case-insensitive, and the argument is matching case-insensitive because of database collation being case-insensive.

3
  • Thank you! It's not outbound_alter, but inbound_alter. Please change that, so that I can accept the answer. Are there any potential side-effects to this? All of my URLs are lowercased and I don't have any worries about that, but are there potential performance issues or strange redirects?
    – take2
    Apr 7 '16 at 16:11
  • @take2 I will update the answer later. This is a bit of a weird situation b/c Drupal 7 inbound URLs are case-insensitive. I think this is a weird Views thing.
    – mpdonadio
    Apr 7 '16 at 16:20
  • I was surprised to learn that even the default Views pages accept both versions (e. g. example.com/viewspage and example.com/ViewsPage).
    – take2
    Apr 7 '16 at 16:30

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.