4

I'm currently blocking access to some admin menu pages using the Rules module as follows:

Events: Drupal is initializing

Conditions:

  • Data comparison > Data to compare:[site:current-page:url] then URL1 to block
  • Data comparison > Data to compare:[site:current-page:url] then URL2 to block
  • Data comparison > Data to compare:[site:current-page:url] then URL3 to block
  • ...

Actions:

  • Show a message on the site
  • Page redirect to home

I want to supply a list of URLs in one field rather than multiple data comparison conditions, is this possible in Rules?

4
  • You need to use PHP Snippet rather than other events. In PHP code use an array, which will have all the blocked URLs and compare request_uri() with this array (array_search() or in_array() will come in handy.
    – GoodSp33d
    Commented Mar 1, 2013 at 14:08
  • Are you saying to do this outside of Rules in a module? Sorry, slightly confused. Commented Mar 1, 2013 at 14:10
  • No you can execute PHP Snippets as part of Rules module, instead of Events you need to choose Execute as PHP Code in the drop down. Test your rules in local environment once as errors in codes can sometimes break your site.
    – GoodSp33d
    Commented Mar 1, 2013 at 14:15
  • @GoodSp33d Using PHP snippets as part of Rules (Conditions or Actions) is something you should try to avoid whenever you can. Have a look at my answer to this question here how you can do so, without PHP. Commented Sep 5, 2016 at 9:06

2 Answers 2

2

Assume the relative paths for your 3 URLs are like my/path1 (for ULR1), my/path2 (for ULR2) and my/path3 (for ULR3). And assume your homepage has a relative path MyHomePage. And assume this question is about D7.

With these assumptions, you could use a rule that answers your question which looks like so (in Rules export format):

{ "rules_limit_access_to_some_urls" : {
    "LABEL" : "Limit access to some URLs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "site:current-page:path" ],
          "op" : "IN",
          "value" : { "value" : [
              "my\/path1",
              "my\/path2",
              "my\/path3"
            ]
          }
        }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "MyHomePage" } },
      { "drupal_message" : {
          "message" : "Access to the content located at \u0022\u003Cstrong\u003E[site:current-page:path]\u003C\/strong\u003E\u0022 is not allowed.",
          "type" : "warning"
        }
      }
    ]
  }
}

Note site:current-page:path token in this rule, which is different from site:current-page:url.

To make the above rule work in similar situations, just adapt the paths by changing 'my/pathX' (with X=1, 2, 3) to whatever fits. And adapt the message to be shown.

Some more samples where you can see this technique at work:

Remark: Instead of using a data comparison in this Rules Condition, you may also want to try using a text comparison. That would allow you to use regular expressions. If your URLs have a common pattern for which you can write to single regular expression (such as URLx, with x=1, 2, 3, ..., 456, 457, ...), you would only need 1 such Rules Condition.

1

You can achieve what are you looking for by using "Execute custom PHP code" as @2-Stroker mentioned.

If you consider Execute custom PHP code as an option to solve your issue, you need to do the following:

  • First enable PHP filter module
  • In your rule add a condition element
  • In condition dropdown list select Execute custom PHP code
  • In the PHP code section add the following code and change URLs accordingly

code:

$urls = array('/admin/whatever-url-1', '/admin/whatever-url-2', '/admin/whatever-url-3');

if(in_array(request_uri(), $urls)) {
  return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.