0

First some introduction:

The hook is working in other pages or direct urls. The only situation where is not working is in the find content page, "admin/content".

When the user clicks on edit link, drupal loads the node edit page, but we need to redirect that edit page to our own edit page. That's why we use the code in the hook.

THE PROBLEM:

When the user clicks on edit, he or she is redirected to the same page again (Find content). Intead of going to the my_module edit page. I Don't know why it's happening this. Any help is welcome :)

The Code:

function my_module_init() {

    // redirect drupal edit page to our own module edit page.
    if (preg_match('/^node\/(\d+)\/edit$/', current_path(), $matches)) {
      $code = _my_module_check_code($matches[1]); //check if the nid is of the content type we need
      if ($code) {
        return drupal_goto('my_module/' . $code . '/edit');
      }
      // If the code is not from the content type we need, we do nothing 
      // just leave drupal work it's way
    }

}
  • Why the downvote? I don't think it's a dumb question. – Salvador P. May 14 '14 at 13:11
  • I upvoted your question because this post helped me too. – Roger Aug 18 '15 at 10:58
4

The solution is to use the hook_preprocess_page().

function my_module_preprocess_page() {

    // redirect drupal edit page to our own module edit page.
    if (preg_match('/^node\/(\d+)\/edit$/', current_path(), $matches)) {
      $code = _my_module_check_code($matches[1]); //check if the nid is of the content type we need
      if ($code) {
        global $base_url;
        $url = $base_url.'/my_module/'.$code.'/edit';
        header('Location: ' . $url);
      }
      // If the code is not from the content type we need, we do nothing 
      // just leave drupal work it's way
    }

}

After clearing cache, all is working fine.

  • You saved my day, thanks! I only want to remark that apart from using hook_preprocess_page is important to replace drupal_goto for header function for this to work (like in your code). – Roger Aug 18 '15 at 10:57
  • Thank you Roger I'm glad this helped you :D Yes you are correct, using header instead of drupal_goto is important for this to work. In drupal 8 drupal_goto() has been removed... – Salvador P. Aug 19 '15 at 9: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.