6

For example, here's a snippet form the Comments Module (comment.module):

function comment_menu() {
  $items['comment/%'] = array(
    'title' => 'Comment permalink',
    'page callback' => 'comment_permalink',
    'page arguments' => array(1),
    'access arguments' => array('access comments'),
  );
  $items['comment/reply/%node'] = array(
    'title' => 'Add new comment',
    'page callback' => 'comment_reply',
    'page arguments' => array(2),
    'access callback' => 'node_access',
    'access arguments' => array('view', 2),
    'file' => 'comment.pages.inc',
  );

  return $items;
}

How can I override $items['comment/%'] with $items['comment/reply/%node'], without hacking core.

In other words, I need example.com/comment/[cid] to look exactly like example.com/comment/reply/[nid]/[cid]?

8

Yes, you should use hook_menu_alter Create new module and add hook_menu_alter

function my_module_menu_alter(&$items) {
    // Unset core path
    unset($items['comment/%']);
    $items['your/pass/here/%node'] = array(
        'title' => 'Add new comment',
        ...
      );
}

But link "comment/reply/%nid/%cid" is used by Drupal for replies to other comments (where %nid - node id and %cid - comment id you are replying to). Use another path.

  • How safe is it to unset the $item? I'm asking, because in some instances you're preferred to use hide(), etc... – timofey.com Oct 10 '12 at 0:57
  • well. really all information about menu callbacks are stored in DB - table "menu_router". This table is updated when you are flushing cache. While flushing - drupal reads all hook_menu and hook_menu_alter functions. So after unset - just you will get row with such callback in this table. – Gaydabura Oct 10 '12 at 1:01
1

You can always use hook_menu_alter and pass the same page callback and arguments.

  • Is there a function that can do that in template.php? – timofey.com Oct 9 '12 at 20:21
  • menu alter should be implemented in a module. I personally am not aware of a way to override the menu at the template level. – junedkazi Oct 9 '12 at 20:28

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.