-6

I need to redirect anonymous user before saving a node in Drupal 7.

For this I created a rule which redirects anonymous users to perform a login/register, but it's creating a node. How can I prevent the node creation before registering?

  • Simply don't give the permission to create nodes to anonymous users. In that way, they have to register and login before creating nodes. – kiamlaluno Oct 17 '15 at 6:23
  • 1
    Also, it doesn't make sense to redirect users to another form right before saving a node because what they changed in the node would be lost. Drupal cannot remember what users entered in a form when users are redirected to another form. – kiamlaluno Oct 17 '15 at 6:26
1

If you want to do this with Rules (as in your question), then it's crucial to use Rules Event "Drupal is initialiazing" (otherwise you're too late, which also explains your "But it's creating a node").

For an example rule (in rules export format) that uses this technique (and also intercepts using a path like /node/add), refer to my answer to "Is there a way to restrict some pages like node/add, /user/login and more?". Just adapt the Rules Conditions in it to fit your needs, i.e:

  • Use role id = 0 instead of 1.
  • Tune the list of paths to be checked.
  • Adapt the message in the Rules Action to fit your needs.

After you do, you should end up with a rule that looks close to this one:

{ "rules_limit_access_to_some_urls_" : {
    "LABEL" : "Limit access to some URLs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "0" : "0" } }
        }
      },
      { "data_is" : {
          "data" : [ "site:current-page:path" ],
          "op" : "IN",
          "value" : { "value" : [
              "node\/add",
              "node\/add\/article",
              "node\/add\/book"
            ]
          }
        }
      }
    ],
    "DO" : [
      { "redirect" : { "url" : "no_access" } },
      { "drupal_message" : {
          "message" : "Access to the path you tried to use is not allowed.\r\n\u003Cbr \/\u003E\r\nIf you think this is an error, please contact us.",
          "type" : "warning"
        }
      }
    ]
  }
}

That's really it.

| improve this answer | |
  • 1
    This is an old question, so the original intent isn't clear. My read of it is that the OP wanted to allow users to be able to fill out a node form, but before it is saved out, force them to register, and then have the node be saved (I have gotten similar requests). Will this solution allow that, or does it just redirect anon users to the registration page when then request a node add path? – mpdonadio Jan 21 '17 at 2:16
0

Here are some solutions you can follow :

  1. As Kiamlaluno mentioned the first solution and the most regular one is to force user to register before being able to see node creation form . you can use Always visible module to make node creation view visible to anonymous users , then in site information part, redirect access denied errors to user login form . in this part modules like logintobaggan or User registration password will become useful for fast registration .After registration user will redirected to node creation form.

  2. Create your own form. In this situation a multi-step form is needed which in the first steps node creation form is implemented and in second step user creation. off course it is not an easy solution and needs information about module development in Drupal.

| improve this answer | |
-1

What about usage of hook_form_alter and drupal_goto? Something like next

function youmodule_form_alter(&$form, $form_id) {
  if ($form_id === 'your_node_form' && user_is_anonumous()) {
    drupal_goto('/user/regiser');
  }
}
| improve this answer | |
  • Can I do this via rule.? – jsh Oct 17 '15 at 6:15
  • Form alteration hooks should not redirect users to other pages. – kiamlaluno Oct 17 '15 at 6:19
  • Where I can found description of it? I tryed to find it now, but not succesfully – Alexander Pozharskii Oct 17 '15 at 6:33
  • 1
    There is a misspelled function: user_is_anonymous – jorgetutor Oct 17 '15 at 8:44

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.