15

I'm using Drupal 8.1 with Rules 8.x-3.0-alpha1.

I want to create a rule to send an email to the author of a node when a new comment is posted to this node. Problem is I didn't get the email address of the author with rules because the dataset gives me only this values: @node.node_route_context:node or comment but under comment I didn't find anything like comment:node:author. In Drupal 7 I have this data. What I have to do in Drupal 8?

  • The alpha status of Rules might mean that this functionality isn't implemented yet, requiring you to patch it yourself. – Bojan Zivanovic May 22 '16 at 18:51
  • 1
    Thanks but it seams to be a problem of the token module. Because other modules like Entity notification doesnt get the author:mail too. This token seems broken. – lolhonk May 23 '16 at 10:48
5

Updated Answer

Whilst testing this on a fresh install of Drupal 8 with Rules (Alpha 2) I have come across a bug which is preventing a successful test of this, whilst the data selector below is correct I am presently unable to reproduce a working rule.

The bug report is here: https://www.drupal.org/node/2770291 - To summarise this issue Rules is expecting the TO: parameter to be an array and will throw an error when passing the email address token below which returns a string.

My initial answer holds true for Drupal 7 however for Drupal 8 the situation seems simpler and hinges on a single fact that isn't yet well-documented.

In D7 we're familiar with a syntax resembling: comment:node:author:mail

In D8 the same selector (which works) is: comment.entity_id.entity.uid.entity.mail.value

Hopes this helps, I'm leaving the previous answer below in case it helps someone else.

Previous Answer

*In general with Rules to use the value of a field the rule must first specifically check for the existence of the field (i.e. that the field has a value in the context the rule is running). If you haven't tried this already you might want to try adding an "Entity has field" condition to the rule to check for the existence of the email address. This should then expose the token for the field.

I've had similar issues executing rules using entity relations (which is more or less what you're doing by referencing the author from the node) checking for the existence of the field (in your case most likely a check that the entity has an author (this should open up access to the user object) then a check against the user object to ensure it has an email address) opened up the available field tokens from the referenced entity.*

  • I'd be interested to see a working rule included in this answer that actually does what this question is about ... for D8. Can you update your anwer to include such example (using the most recent D8 version of the Rules module? PS: from the number of upvotes of this question, it seems that there are other users also looking for something similar ... – Pierre.Vriens Nov 16 '16 at 20:29
  • Sure thing and great idea, It'll take me a little time to spin up a fresh test site to produce this. – HomoTechsual Nov 16 '16 at 21:29
  • Can't wait to see your updated answer soon. Though spinning up an appropriate site is really only 1 click away ... use https://simplytest.me/project/rules, accept the (default) version of 8.x-3.0-alpha2 and hit the "launch sandbox"-button. After you do, you have everything in place you'd need to "quickly" construct the Rule you have in mind. Then export it, and copy/paste that export in your answer here. Then let me know (extra comment?) your rule is ready for QA-review. Would be nice if you could post it somewhere in the coming days ... if you can. – Pierre.Vriens Nov 16 '16 at 22:14
  • Hi @Pierre.Vriens I've edited my comment, after playing with Rules on D8 a little further this turns out to be less complex that I thought and more a result of changed selectors in D8. – HomoTechsual Nov 16 '16 at 22:22
  • Also I'm not sure why I always forget about SimplyTest.me – HomoTechsual Nov 16 '16 at 22:26
1

There's a working example in the Rules issue queue that was posted in November 2016, so this has been possible at least since then. See https://www.drupal.org/project/rules/issues/2828422

Here's a slightly modified version of that answer. You may import this into your site and look at it in the UI to learn how to configure this functionality.

langcode: en
status: true
dependencies: {  }
id: 'email_node_author_on_new_comment'
label: 'Email Node Author on New Comment - Issue 2828422'
events:
  -
    event_name: 'rules_entity_insert:comment'
description: 'Sends email to Node Author when a New Comment is added.'
tags:
  - node
  - email
  - comment
config_version: '3'
expression:
  id: rules_rule
  uuid: b5276280-d0f2-4f64-9512-06f077793c58
  conditions:
    id: rules_and
    uuid: ebf212fc-d858-417d-977d-af1be2cfd886
    conditions: {  }
  actions:
    id: rules_action_set
    uuid: 88a78280-2bca-4367-a4d3-8bbda9397c2a
    actions:
      -
        id: rules_action
        uuid: 5e6e5aed-b75f-412a-9670-48327eb58ba6
        context_values:
          to:
            - '{{ comment.entity_id.entity.uid.entity.mail }}'
          subject: 'Your content {{ comment.entity_id.entity.title }} has a new comment.'
          message: 'Your content {{ comment.entity_id.entity.title }} has a new comment.'
          reply: ''
          language: ''
        context_mapping: {  }
        context_processors:
          to:
            rules_tokens: {  }
          subject:
            rules_tokens: {  }
          message:
            rules_tokens: {  }
          reply:
            rules_tokens: {  }
          language:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_send_email

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.