Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implements hook_hook_info().
- */
- function comment_paths_hook_info() {
- $info['comment_paths'] = array('group' => 'comment_paths');
- return $info;
- }
- /**
- * Implements hook_help().
- */
- function comment_paths_help($path, $arg) {
- switch ($path) {
- case 'admin/help#comment_paths':
- $output = '<h3>' . t('About') . '</h3>';
- $output .= '<p>' . t('Allow custom paths for comments') . '</p>';
- return $output;
- }
- }
- /**
- * Implements hook_boot().
- * To make the module load prior to bootstrap
- */
- function comment_paths_boot() {}
- /**
- * Implements hook_url_inbound_alter().
- */
- function comment_paths_url_inbound_alter(&$path, $original_path, $path_language) {
- // check if this is a comment URL
- if(preg_match('@^([^\?]+)/comment/(add|edit|reply|delete)(/(\d+))?$@', $original_path, $matches)) {
- // Get the internal path of the node (blog/title)/comment/action
- // Where blog/title is the alias of the node
- $source_path = drupal_lookup_path('source', $matches[1]);
- if(!$source_path) {
- return;
- }
- // Get the node ID
- preg_match('@^node/(\d+)$@', $source_path, $node_match);
- $nid = isset($node_match[1]) ? intval($node_match[1]) : 0;
- if(!$nid) {
- return;
- }
- $node = node_load($nid);
- // Stop if no node is found
- if(!$node) {
- return;
- }
- // Actions that requires comment ID
- $comment_id_actions = array('edit', 'reply', 'delete');
- // 'Add' actions, requires no comment ID
- $comment_add_actions = array('add');
- // Set the action (edit/reply/delete/add)
- $action = $matches[2];
- $comment_id = isset($matches[4]) ? intval($matches[4]) : 0;
- // Change the path for comment ID actions
- if(in_array($action, $comment_id_actions) && $comment_id) {
- $path = 'comment/' . ($action == 'reply' ? "reply/{$node->nid}/{$comment_id}" : "{$comment_id}/{$action}");
- }
- // Change the path for add actions
- if(in_array($action, $comment_add_actions)) {
- $path = "comment/reply/{$node->nid}";
- }
- }
- }
- /**
- * Implements hook_url_outbound_alter().
- */
- function comment_paths_url_outbound_alter(&$path, &$options, $original_path) {
- // Match paths starting with comment/*
- if(preg_match('@^comment/(reply|\d+)(/(\w+|\d+)(/(\d+))?)?@', $path, $matches)) {
- // If action is reply the structure in the URL is a little different from the other actions
- if($matches[1] === 'reply') {
- // If there is no nodeID, exit the function
- if(!isset($matches[3]) || !preg_match('@^[0-9]+$@', $matches[3])) {
- return;
- }
- $nid = intval($matches[3]);
- $action = 'reply';
- // If commentID is defined, extract it
- if(isset($matches[5])) {
- $cid = intval($matches[5]);
- } else {
- $action = 'add';
- }
- } else {
- // All the other actions should have a commentID
- $cid = intval($matches[1]);
- if(count($matches) > 3) {
- $action = $matches[3];
- }
- }
- // If there is no nodeID from the URL, load it from the comment
- if(!isset($nid)) {
- $comment = comment_load($cid);
- if(!$comment) {
- return;
- }
- $nid = $comment->nid;
- }
- // Build path from the node alias
- $path_builder = drupal_get_path_alias("node/{$nid}");
- // If no action is set, it's a comment permalink
- if(!isset($action)) {
- $path = $path_builder;
- $options['fragment'] = "comment-{$cid}";
- return;
- }
- $path_builder .= "/comment/{$action}";
- // Add the commentID, only 'add' will not have this
- if(isset($cid)) {
- $path_builder .= "/{$cid}";
- }
- $path = $path_builder;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement