14

Standard Drupal behavior is to pass all arguments through, whether or not they requested. Sometimes this behavior is desired. However, I need to return a 404 for anything but a query string.

Example of the problem http://drupal.org/project/modules http://drupal.org/project/modules/google.com <- This should return a 404; 200 given. Duplicate content penalty!

The views 404 module assumes that by default you do not want arguments passed through. However there is no D7 version of the module.

But the project page says that 404s for views can also be accomplished by setting the Global: Null under Arguments.

Obviously in D7 "arguments" are now "contextual filters", but I am unsure as to what settings to add to the Global:null contextual filter to get my desired result.

Any help would be appreciated.

22
+50

In Contextual filters select Add; in the dialog box that appears, select Global: Null.

screenshot

Click on Add and configure contextual filters.

In the dialog box that appear next, in the fieldset When the filter value is available or a default is provided, check Specify validation criteria, select - Basic validation - for Validator, and select Show "Page not found" for Action to take if the filter value does not validate. In the fieldset More, check Fail basic validation if any argument is given.

screenshot

I tested it with an "Articles" view I created, and it works.

screenshot

screenshot

The "Global: Null" argument must be the last one. If you need a user ID, for example, first you add it as argument, and then add the "Global: Null" argument.

I altered the "Article" view to accept a user ID as argument, which is used to filter the list of article nodes. The view still works, as it doesn't accept an extra argument.

screenshot

screenshot

  • 1
    This will return 403 not 404 code error. Select "Show "Page not found" option instead – Eugene Fidelin Oct 23 '12 at 9:44
  • In a thread on D.O. I see someone suggesting to use arg(1)==NULL; in the php validate code. Is this better or pretty much the same thing? – Nigel Waters Oct 23 '12 at 17:53
  • 1
    @EugeneFidelin I was editing the "master" display, and I was not seeing the Show "Page not found" as option. I corrected my answer. – kiamlaluno Oct 23 '12 at 20:00
  • @NigelWaters Forget my previous answer. :) I was editing the wrong display, and the view was not working as it should. There is no need to use PHP code to validate the argument; see the updated answer. – kiamlaluno Oct 23 '12 at 20:02
  • I don't have enough points to comment yet - so posting here to say thanks to @kiamlaluno. Works great and something I was banging my head against for a while. – Blue Waters Mar 30 '13 at 11:24
1

You can do this using views out of the box functionality. In every contextual filter setting you can see a check-box "Specify validation criteria" check that to see more settings.

Then, you can select "validator" based on your view conditions. If you would like to custom validate then select php code. Return True or False in php code based on your validation conditions.

Finally select "Action to take" as Display Access denied ! See below screenshot for nid contextual filter.

enter image description here

  • What is the php code that I add? – Nigel Waters Oct 18 '12 at 19:06
  • I am only using the Global:null contextual filter – Nigel Waters Oct 18 '12 at 20:44
  • 3
    NEVER USE PHP CODE IN VIEWS OR ANY OTHER UI TOOL!!! – hugronaphor Feb 6 '15 at 11:17

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.