Rules is a tool that enables you to define automatic, conditionally executed actions, triggered by various types of events. the basic outline of this functionality is:
- WHEN an event happens
- IF conditions are right
- THEN take an action
Install Rules
Run the following from the command line in the root of your project composer require drupal/rules
Once this has run you can return to the Drupal dashboard and enable the Rules module.
Create the Rule
To create a rule navigate to /admin/config/workflow/rules
then click on the Add a reaction rule button. Fill in the details with the following.
Label: send email after adding content
React on event: After saving a new content item
Add the Conditions
Now we need to add a condition so click the Add condition button.
From the select box choose Entity is of bundle
from under Content
and then click Continue.
When adding this condition it is useful to Switch to data selection for the Data selector input. The Data selector we want is node
Set the Type value to node
Set the Bundle value to article
. This is the machine name of Article content type.
We can now save the condition.
Add the Actions
We can now add an action to Send email after adding article content. Click the Add action button and choose Send email
from the System section of the dropdown.
Enter the send to, subject and message values.
We can now save the action. if you want to make message field as textarea in send email action, you can apply this patch rules_sendmail_textarea-2724129-46.patch.
see this link https://www.drupal.org/patch/apply if you don't know how to apply a patch in drupal.
Next steps
- Clear your Drupal 8 caches. To do this I use this Drush command:
drush cr
if you don’t currently use Drush, I highly recommend using it, or the Drupal Console. - Now log in and check if the reaction rule is working.
- I hope you found this post useful. let me know if you have any questions and I’ll be happy to answer them.