0

It seems that the standard Drupal behaviour for unpublished nodes is this: If a visitor types in a URL to an unpublished node they are redirected to a login page. To -me- that signals that the content is 'there' but they just need credentials.

So: Is there a way to change this behaviour so that if they type in a URL to an unpublished node, literally -nothing- happens? Or perhaps they are redirected to the home page?

Below is my attempt to redirect to home page. The page still redirects to the Access Denied. What I don't get is that if I alter the Rule so that it tests for role 'Administrator' it works -fine-. So apparently an Anonymous user has some special property?

{ "rules_redirect_unpubished_content_url_to_home_page" : { "LABEL" : "Redirect Unpubished Content URL To Home Page", "PLUGIN" : "reaction rule", "OWNER" : "rules", "REQUIRES" : [ "rules" ], "ON" : { "node_view--bags_cases" : { "bundle" : "bags_cases" } }, "IF" : [ { "NOT node_is_published" : { "node" : [ "node" ] } }, { "user_has_role" : { "account" : [ "site:current-user" ], "roles" : { "value" : { "1" : "1" } } } } ], "DO" : [ { "redirect" : { "url" : "/" } } ] } }

I also tried this:

function jch_utilities_node_view($node, $view_mode, $langcode) { if ($node->status != 1) { drupal_goto('/some-page'); } }

...and again, the 403 fires before this has a chance to run

Finally, I tried hook_init, but yet again, the 403 fires before this has a chance to run.

What am I missing?

  • The default behaviour is an "Access denied" page, because a visitor hasn't access to unpublished nodes. – arrubiu Jan 17 '15 at 8:37
2

The default behaviour is an "Access denied" page, because a visitor hasn't access to unpublished nodes. It's not possible "nothing appens". You could use Rules and create a Rule like this:

  • event: viewing a node
  • condition: user has role "anonymous"
  • condtion: node is unpublished
  • action: redirect to a custom page
  • My understanding is that an anonymous user doesn't have a Role. Can you explain what Condition I use to test for Anonymous users? – jchwebdev Jan 19 '15 at 20:34
  • The role is a special role called "Anonymous". Go to admin/people/permissions/roles to see all the roles you have. Notice the 2 uneditable roles called Anonymous and Authenticated. Those are special roles used to identify logged in/anonymous users. – Beebee Jan 20 '15 at 0:18
  • I understand that, but I can't get the rule to fire. Can you (someone) look at the Rule I posted above and tell me why it isn't working? – jchwebdev Jan 22 '15 at 19:09
0

this module will allow you to:

"setup redirects for each node type on their site. They can also set different types of redirects per node type. Developers can alter the node type list if required"

https://www.drupal.org/project/unpublished_nodes_redirect

0

You could actually use this to get the node status:

if ($node = menu_get_object()) {
  // Get the status 
  $status = $node->status ;
}

Your final module would then be something like this:

function your_module_init() {
  if (!function_exists('drush_main')) { // prevents issue with drush
    if ($node = menu_get_object()) {
      if ($node->status != 1) {
        drupal_goto('some-page'); // without the preceding /
      }
    }
  }
}

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.