Score:0

How to access node entity reference on comment save via rules?

tk flag

I have a content type with a user reference field on it, the users are populated here by a view.

When a user saves a comment, I want to access that field and send an email to user in the entity reference field.

rd89 avatar
tk flag
Going to post this here, answering my own question. Not sure if this is hacky or not but I worked around my inability to access the node fields by having the comment update the node changed to the current time. Then created a new rule to fire an email off to the entity reference user on that update. ¯\_(ツ)_/¯
hk flag
That is definitely hacky :).
rd89 avatar
tk flag
@anoopjohn want to send a little guidance? I will send a cup of coffee for your effort :)
Score:-1
hk flag

Although the question is about doing this with rules, here is how you can do this with some custom code. There could be a rules based approach towards doing the same thing.

You can implement a hook_ENTITY_TYPE_insert hook in your module which will get called when a comment is inserted and then from that comment, get the node and from there you can do whatever you want to do with the node.

function custom_module_comment_insert(\Drupal\Core\Entity\EntityInterface $entity) {
   // $entity will be a comment entity.
   $node = $entity->getCommentedEntity();
   if ($commented_entity && $commented_entity instanceof \Drupal\node\NodeInterface) {
     // Do what you want to do with the node
   }
}

Hope this helps somebody running into this post.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.