0

My D7 customer uses the Workbench suite of modules to use taxonomy to allow fine-grained access to nodes successfully. But with a large amount of data to manage, sometimes they want to unpublish a node for the time being while keeping the data available in case they want to publish again. In Workbench, I've set up an "Archived" option which successfully renders that content unavailable to anonymous users. So when anonymous users visit that node, workbench, using hook_node_access() renders that node to NODE_ACCESS_DENY and it appears Drupal takes over and redirects the user to /user. That's good, but the customer wants to redirect back to root if they are anonymous. I thought about modifying Workbench using their API but it seems to be after stepping through how Drupal handles the menu_execute_handler() that the redirection to /user occurs. I need workbench to return the DENY so I can use that as one condition to dynamically alter how Drupal renders the view in a DENY situation. But I haven't found which part of core handles this redirection. Hunting through searches I found this suggestion which suggests to handle all 403s.

function my_module_menu() {
  $items['403redirect'] = array(
    'page callback' => 'mymodule_unpublished_redirect',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    );
return $items;
}

function mymodule_unpublished_redirect() {
    if (<TRUE>) {
        header('location:'. url('/', array('absolute' => TRUE)), TRUE, 302);
    }
}

But when visiting the node that is "Archived" redirects to /user which suggests that this function is ignored at this point. So how should I alter this redirect?

2
  • "For this to work, you have to set the 403 page under Administer > Site Configuration > Error Reporting" (from the linked suggestion) Did you do that? Commented Nov 19, 2018 at 15:39
  • That points me to what is likely the solution. In that dialog I can point to a URL that will be in this custom module and can make a better callback in deciding how to handle the redirect. If you would form into an answer, I'll choose it. Thx.
    – sam452
    Commented Nov 19, 2018 at 19:04

1 Answer 1

2

The idea of creating a menu entry to redirect in the case of a 403 is fine, except that you need to configure the core handler to point to it, which is the element missing from your question (it's mentioned in the comment you linked to).

Once you have defined your menu entry go to Administer > Site Configuration > Error Reporting and set the value of the 403 page to the corresponding path.

Now the page callback for your menu entry will be invoked whenever there is access denied, so you can supply the appropriate logic for your needs.

1
  • For Drupal 8 and 9, Admin Panel -> Configuration -> System -> Basic site settings.
    – Harish ST
    Commented Oct 14, 2020 at 16:23

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.