1

Currently I'm trying to add create a rule that simply redirects anyone from /node/add to /user/login. It's intended to be part of a larger rule, but I'm having No luck with this

Steps of the rule are

  1. Trigger - User is going to view a page
  2. If path has alias(es) - /node/add
  3. Do page redirect to - /user/login

Instead, it just goes to the /node/add page as usual. Why is this simple rule is not working?

Here is the rule exported.

array (
  'rules' => 
  array (
    'rules_30' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_init',
      '#label' => 'Redirect to login if accessing /node/add',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        1 => 
        array (
          '#weight' => -1,
          0 => 
          array (
            '#weight' => 0,
            '#info' => 
            array (
              'label' => 'Path has alias(es)',
              'module' => 'Path',
              'eval input' => 
              array (
                0 => 'src',
              ),
            ),
            '#name' => 'rules_condition_url_has_alias',
            '#settings' => 
            array (
              'src' => 'node/add',
            ),
            '#type' => 'condition',
          ),
          '#type' => 'OR',
        ),
      ),
      '#actions' => 
      array (
        1 => 
        array (
          '#weight' => 0,
          '#info' => 
          array (
            'label' => 'Page redirect /user/login',
            'label callback' => false,
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'path',
              1 => 'query',
              2 => 'fragment',
            ),
          ),
          '#name' => 'rules_action_drupal_goto',
          '#settings' => 
          array (
            'path' => '/user/login',
            'query' => '',
            'fragment' => '',
            'force' => 0,
            'immediate' => 0,
          ),
          '#type' => 'action',
        ),
      ),
      '#version' => 6003,
    ),
  ),
)
0

2 Answers 2

3

One easy option is to set the 403 Access Denied page at:

admin/config/system/site-information

to:

user

That way, if the user is not authenticated, they can login and view the node/add page.

I think the reason your Rule isn't working is because /node/add isn't an "alias" per se, but a system path.

Another option is something like

/**
 * Implements hook_init
 */
function MODULENAME_init(){
  if($_GET['q'] == 'node/add' && !user_is_logged_in()){
    drupal_goto('user/login');
  }
}
1

A more general solution is available by using the LoginToboggan module. WHen you configure LoginToboggan at /admin/user/logintoboggan simply Enable the option to Present login form on access denied (403).

As far as the rule is concerned, node/add is a system path, not an alias, and it leads to the Add Content page. You might be able to get the rule to work if you use a data comparison on node/add/* but if the user does not have access I doubt the rule will trigger.

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.