Advertisement
thebreiflabb

Comment Paths

Jun 10th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Implements hook_hook_info().
  5.  */
  6. function comment_paths_hook_info() {
  7.   $info['comment_paths'] = array('group' => 'comment_paths');
  8.   return $info;
  9. }
  10.  
  11. /**
  12.  * Implements hook_help().
  13.  */
  14. function comment_paths_help($path, $arg) {
  15.   switch ($path) {
  16.     case 'admin/help#comment_paths':
  17.       $output = '<h3>' . t('About') . '</h3>';
  18.       $output .= '<p>' . t('Allow custom paths for comments') . '</p>';
  19.       return $output;
  20.   }
  21. }
  22.  
  23. /**
  24.  * Implements hook_boot().
  25.  * To make the module load prior to bootstrap
  26.  */
  27. function comment_paths_boot() {}
  28.  
  29. /**
  30.  * Implements hook_url_inbound_alter().
  31.  */
  32. function comment_paths_url_inbound_alter(&$path, $original_path, $path_language) {
  33.   // check if this is a comment URL
  34.   if(preg_match('@^([^\?]+)/comment/(add|edit|reply|delete)(/(\d+))?$@', $original_path, $matches)) {
  35.     // Get the internal path of the node (blog/title)/comment/action
  36.     // Where blog/title is the alias of the node
  37.     $source_path = drupal_lookup_path('source', $matches[1]);
  38.  
  39.     if(!$source_path) {
  40.       return;
  41.     }
  42.  
  43.     // Get the node ID
  44.     preg_match('@^node/(\d+)$@', $source_path, $node_match);
  45.     $nid = isset($node_match[1]) ? intval($node_match[1]) : 0;
  46.  
  47.     if(!$nid) {
  48.       return;
  49.     }
  50.  
  51.     $node = node_load($nid);
  52.  
  53.     // Stop if no node is found
  54.     if(!$node) {
  55.       return;
  56.     }
  57.  
  58.     // Actions that requires comment ID
  59.     $comment_id_actions = array('edit', 'reply', 'delete');
  60.  
  61.     // 'Add' actions, requires no comment ID
  62.     $comment_add_actions = array('add');
  63.  
  64.     // Set the action (edit/reply/delete/add)
  65.     $action = $matches[2];
  66.     $comment_id = isset($matches[4]) ? intval($matches[4]) : 0;
  67.  
  68.     // Change the path for comment ID actions
  69.     if(in_array($action, $comment_id_actions) && $comment_id) {
  70.       $path = 'comment/' . ($action == 'reply' ? "reply/{$node->nid}/{$comment_id}" : "{$comment_id}/{$action}");
  71.     }
  72.  
  73.     // Change the path for add actions
  74.     if(in_array($action, $comment_add_actions)) {
  75.       $path = "comment/reply/{$node->nid}";
  76.     }
  77.   }
  78. }
  79.  
  80. /**
  81.  * Implements hook_url_outbound_alter().
  82.  */
  83. function comment_paths_url_outbound_alter(&$path, &$options, $original_path) {
  84.   // Match paths starting with comment/*
  85.   if(preg_match('@^comment/(reply|\d+)(/(\w+|\d+)(/(\d+))?)?@', $path, $matches)) {
  86.  
  87.     // If action is reply the structure in the URL is a little different from the other actions
  88.     if($matches[1] === 'reply') {
  89.  
  90.       // If there is no nodeID, exit the function
  91.       if(!isset($matches[3]) || !preg_match('@^[0-9]+$@', $matches[3])) {
  92.         return;
  93.       }
  94.  
  95.       $nid = intval($matches[3]);
  96.       $action = 'reply';
  97.  
  98.       // If commentID is defined, extract it
  99.       if(isset($matches[5])) {
  100.         $cid = intval($matches[5]);
  101.       } else {
  102.         $action = 'add';
  103.       }
  104.     } else {
  105.       // All the other actions should have a commentID
  106.       $cid = intval($matches[1]);
  107.  
  108.       if(count($matches) > 3) {
  109.         $action = $matches[3];
  110.       }
  111.     }
  112.  
  113.     // If there is no nodeID from the URL, load it from the comment
  114.     if(!isset($nid)) {
  115.       $comment = comment_load($cid);
  116.  
  117.       if(!$comment) {
  118.         return;
  119.       }
  120.  
  121.       $nid = $comment->nid;
  122.     }
  123.  
  124.     // Build path from the node alias
  125.     $path_builder = drupal_get_path_alias("node/{$nid}");
  126.  
  127.     // If no action is set, it's a comment permalink
  128.     if(!isset($action)) {
  129.       $path = $path_builder;
  130.       $options['fragment'] = "comment-{$cid}";
  131.       return;
  132.     }
  133.  
  134.     $path_builder .= "/comment/{$action}";
  135.  
  136.     // Add the commentID, only 'add' will not have this
  137.     if(isset($cid)) {
  138.       $path_builder .= "/{$cid}";
  139.     }
  140.  
  141.     $path = $path_builder;
  142.   }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement