3

Is there any way in drupal to redirect comment permalink to their corresponding node paths or url alias?

I want to redirect all comment/x paths to their corresponding node-path. And it should handle pagers as well. If your comment is in the second page (when you have set a comments-per-page value in node type settings), it should be like

http://example.com/comment/111 to http://example.com/node/123?page=1#comment-111

http://example.com/comment/111 to http://example.com/node/123#comment-111
http://example.com/comment/112 to http://example.com/node/123#comment-112

Thanks in Advance!

2
  • It can be done programmatically I think. By using hook_preprocess_page. Oct 22, 2014 at 10:10
  • @SumitMadan Thanks for ur comment. But I somehow managed to acheive it through hook_menu_alter Oct 22, 2014 at 11:22

2 Answers 2

0

I managed to acheive it through hook_menu_alter. It just works great for me

<?php
/**
* Implements hook_menu_alter.
* @author Md Eqbal Ahmad <*****@******.com>
*/
function MODULE_menu_alter(&$items) {
  $items['comment/%']['page callback'] = 'MODULE_commentpermalink';
}

/**
 * Redirect comment permalink to their corresponding node paths
 * @author Md Eqbal Ahmad <*****@*****.com>
*/
function MODULE_commentpermalink($cid) {
  if (($comment = comment_load($cid)) && ($node = node_load($comment->nid))) {
    // Find the current display page for this comment.
    $page = comment_get_display_page($comment->cid, $node->type);
    // Now, rather than setting menu_executive_active_handler and changin $_GET valies, we redirect them to the actual node URL with fragments and pager set.
    if ($page > 0) {
      drupal_goto("node/$comment->nid", array('fragment'=>"comment-$comment->cid",'query'=>array('page'=>$page)), 301);
    }
    else{
      drupal_goto("node/$comment->nid", array('fragment'=>"comment-$comment->cid"), 301);
    }
  }
  drupal_not_found();
}

?>
6
  • I thinks, this is not a right way to do. hook_page_process is more better way than this. hook_menu_alter means you are altering the page content. Oct 22, 2014 at 11:29
  • @SumitMadan I will be highly obliged, if possible, if u provide a code snippet using hook_page_process to redirect comment permalink to their corresponding node paths as ur Answer to the question Oct 22, 2014 at 11:40
  • checkout my answer. Oct 22, 2014 at 14:59
  • @SumitMadan Thanks for ur answers! I have a question that dont u think redirection at hook_page_process has a slow page loading issue. The bulk content array is generated and then u are opting for redirection to load the same page content. Oct 22, 2014 at 15:12
  • I dont think so. I never found any issue with this option. Oct 22, 2014 at 15:19
0

Try it using hook_preprocess_page :

function MODULE_preprocess_page(&$vars) {
      if (arg(0) == 'comment') {
        $cid = arg(1);
        if (($comment = comment_load($cid)) && ($node = node_load($comment->nid))) {
          // Find the current display page for this comment.
          $page = comment_get_display_page($comment->cid, $node->type);
          // Now, rather than setting menu_executive_active_handler and changin $_GET valies, we redirect them to the actual node URL with fragments and pager set.
          if ($page > 0) {
            drupal_goto("node/$comment->nid", array('fragment'=>"comment-$comment->cid",'query'=>array('page'=>$page)), 301);
          }
          else{
            drupal_goto("node/$comment->nid", array('fragment'=>"comment-$comment->cid"), 301);
          }
        }
        drupal_not_found();
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.