Is it possible to load the node on which a nodecomment was made with token?

CommentFileSizeAuthor
#7 nodecomment-tokens.patch1.63 KBhexblot

Comments

mitchell’s picture

Or, is it possible to save the node-comment target as a node reference?

webchick’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev

I'd like this too.

merlinofchaos’s picture

Token isn't very good at having multiple nodes. Maybe with token 2.x

vood002’s picture

Would love to see this.

I built a discussion rss feed which aggregates all of the node comments (and forum topics) on my site--only problem is if you click through the link it takes you to the comment node, rather than it's parent node.

If the parent NID were a token, I'm assuming I could use pathauto to this effect: node/[pid]#reply-[nid]

EDIT now that i think about it the pathauto way definitely won't work. I may be able to achieve it with views, i'll post if I find a good way.

If there's another way to do this I'd appreciate advice as well.

friolator’s picture

@vood002:

We have a custom breadcrumb trail for certain sections of our site, that's based on the current path. We're still using paths that contain named anchors, for the nodecomment view of all the comments attached to a node. This is there so you can make a link to that comment in the context of the overall thread. The big problem for us was in search results for the site. Nodecomment nodes would be linked to via their node id (node/nid), but we needed for the path to show the context of the comment, in order to work inside of our breadcrumb trail. This is especially important with search results, where you'd be taken to the nodecomment as a standalone post. The breadcrumb becomes critical for letting you know where you are.

Our breadcrumbs would come out all weird if the path generated by pathauto was 'forum/comment-title' vs 'forum/container/forum/node-title/comment-title' -- in the case of the latter, we can construct a breadcrumb that lets the user navigate back up easily. Also, we needed that for the case where (in search results), you're linked directly to the node.

Ok, that said, I did this:

<?php
function cd_misc_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
    case 'node':
      if ($object->type == "forum_reply"){
        //determine the nodecomment's parent's path, pass it along to Token.
        $values['node-comment-parent-path'] = drupal_lookup_path('alias', 'node/'.$object->comment_target_nid);
      }
      break;    
  }
  return $values;
}

function cd_misc_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['node-comment-parent-path']    = t('The path to the parent of this node comment.');
    return $tokens;
  }
}
?>

So, this gives us a [node-comment-parent-path] token for use in pathauto. What we've done is set up the following paths:

Forum Topic: forum/[termpath-raw]/[title-raw] (gives us 'forum/container/forum/node-title')
Forum Reply: [node-comment-parent-path]/[title-raw] (gives us 'forum/container/forum/node-title/comment-title')

So far, this seems to work fine. I suppose this could be patched into nodecomment itself if people find it useful enough. The only catch I can see is that "forum_reply" is specific to our site. I guess it should just check for the existence of $object->comment_target_nid, and if it's there, go ahead and make this token. (unless there's a better way)

crea’s picture

Status: Active » Closed (fixed)

Closing all issues. Please reopen if still relevant.

hexblot’s picture

Category: support » feature
Status: Closed (fixed) » Needs review
StatusFileSize
new1.63 KB

This is a patch that is based on the above post by friolator. It patches the nodecomment.module file to add three new tokens that I needed in my advanced forums:

  • [node-comment-parent-path] -> The path to the parent of this node comment. ( eg content/my-post )
  • [node-comment-parent-nid] -> The node id of the parent node ( eg 2315 )
  • [node-comment-forumstyle-path] -> The path to the comment based on the parent node, pagination aware ( eg content/my-post#comment-2467 or content/mypost?page=2#comment-2499 )

The patch can be applied to both the current 6.x-2.x-dev version ( packaged on 2010-07-20 ) or 6.x-2.0-beta6 without problems. I have only used it in the staging environment of a large forum, but will soon go live with it as I don't expect any odd behavior.

I hope this helps someone, it proved invaluable for my forum moderation ( sending emails via flag actions to mods about flagged comments ).

benjifisher’s picture

Version: 6.x-2.x-dev » 6.x-2.0-beta6

I applied the patch, but it does not solve my problem. I would like these tokens to be available when the nodecomment is created. The reason is that my nodecomment includes an ImageField, and I would like to use the title of the parent node as the default ALT text. (I figure that if the rest is working, I can figure out how to add a [node-comment-parent-title] token.)

benjifisher’s picture

I solved my problem, but I do not know enough to do it right. Maybe someone else is willing to do a better job with this.

Step 1
I installed the patch from Comment #7. Three new tokens were defined, but not available in the context I wanted.
Step 2
I installed the ImageField Tokens module (and one other prerequisite module). Now the new tokens are listed when creating a new NodeComment with an ImageField. Unfortunately, the tokens are not replaced when the NodeComment is created.
Step 3
I replaced the line
  if ( $object->type == $object->comment_type ) { 

with if(1) {. After this change, the new tokens actually work. As I said, I do not know enough to do this correctly.

Step 4
I added
$tokens['node']['node-comment-parent-title'] = t('The title of the parent node.');
to nodecomment_token_list() and
  $parent = node_load($object->comment_target_nid);
  $values['node-comment-parent-title'] = $parent->title;

to nodecomment_token_values(). (These functions are defined in the patch from Comment #7.) Success!

I am a little afraid that $parent->title is not properly sanitized, but maybe that is handled when the page is displayed.

IckZ’s picture

hey,
this works very well since I edit and save the node. If I do so, the pattern is not filled and in plaintext in the url.. Any Ideas how to fix this?

benjifisher’s picture

Please be more specific about what you did, what worked, and what did not work. For example, when you say, "this works very well," what does "this" mean?

YK85’s picture

Hello,

Would this feature help with #1071194: Rules Integration for nodecomments? At the moment it seems like it is not possible to reference the node which nodecomment is a comment of. Therefore, in my use case, I am unable to reward userpoints to the commented content's author.

Example:
nodecomment created >> give 5 userpoints to the author >> also give 1 userpoint to the commented content's author

Thank you very much

jasom’s picture

I dont know how to create patch so that im sending all code described and working in #9 and #7


/**
 * Generate relevant tokens
 * 
 */
function nodecomment_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'node':
      // Nodecomments have themselves listed as comment types
      if(1) {
        $values['node-comment-parent-nid']      = $object->nid;
        $values['node-comment-parent-path']     = drupal_lookup_path('alias', 'node/'.$object->comment_target_nid);
        $parent                                 = node_load($object->comment_target_nid);
        $values['node-comment-parent-title']    = $parent->title;
        
        // Get the page the comment is located at
        $page = nodecomment_page_count($object);
        if($page) { 
          $page_path = '?page='.$page;
        } else {
          $page_path = "";
        }
        $values['node-comment-forumstyle-path'] = $values['node-comment-parent-path'].$page_path.'#comment-'.$object->nid;
      } 
    break;
  }

  return $values;
}

function nodecomment_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['node-comment-parent-path']       = t('The path to the parent of this node comment.');
    $tokens['node']['node-comment-parent-nid']        = t('The node id of the parent node.');
    $tokens['node']['node-comment-forumstyle-path']   = t('The path to the comment based on the parent node.');
    $tokens['node']['node-comment-parent-title']      = t('The title of the parent node.');
    return $tokens;
  }
}

YK85’s picture

Thanks!!

eidolon night’s picture

Used #18 with the latest Pathauto 2.x and it looks like stuff is getting encoded... twice. Here's an example:

#comment -> %23comment -> %2523comment

aCCa’s picture

See #16 -> With rules you can. Just Populate the node reference field after a nodecomment has been saved and return a php snippet like this one:

return array(0=>array('nid' => $your_nodecomment_type->comment_target_nid),);
tbugert’s picture

#21
I also need to react from a nodecomment with its Parent-Node. But I'm not sure, if I understand your tip. Where do i put the nodereference field: to the Parent node or to the comment node?

bkosborne’s picture

Just putting my two cents in, I needed this functionality and I have it working.

I have product reviews as a node comment, and when a user submits one, I wanted the title of the review to be "Review: [product_name]". So I modified #17 a bit, created my own module for it, and have it working with automatic node title module.

sirkirya’s picture

Category: feature » bug

I've got the node on which a nodecomment was made with help of this Custom tokens module. The settings for the newly created custom token are:

Token ID: token_custom_node_comment_target (or any other name ^_^)
Type: Node
PHP Replacement: return $node -> comment_target_nid;

Later I've used that token with Rules.

Sorry for my poor English skills.

And sorry for inappropriate issue settings.

userok’s picture

subscribe

crea’s picture

Category: bug » feature
Status: Needs review » Needs work

First, this is not a bug, unless the token was implemented in the module from the start
Second, I don't see working patch - there are no success reports.

crea’s picture

Version: 6.x-2.0-beta6 » 6.x-3.x-dev