Skip to main content
PK's Diary

Main navigation

  • Home
  • Reposts
  • Stories
  • Articles
  • Notes
  • About

Breadcrumb

  1. Home
  2. Notes

Try to use Rules module or Rules API for feature like sending mail based on any event

Problem Statement: Recently, in my project, I came across a situation where on a webform submit by the authenticated user I had to send mail based on a configured variable. So, the condition was if variable is set to value 1 mail should be sent to site admin, else if it is configured 2 mail should be sent to the users which were referenced from the current user via field (say field_user_references) and tricky part if user field (field_user_references) is empty, mail should go to the site admin only.

Now, first obvious, easy and probably perfect solution comes to the mind is custom. Let's make a form alter and add our custom submit handler and play with the code to achieve functionality, quite easy!!!

But, what if we achieve this via Rules module.

 

Possible Solution: Awesome thing about Drupal is we have 32k+ contrib modules. There is a module Webform Rules which basically will help us to get webform submitted action. Now we want our rules to basically create conditions based on the variables configured. These variables are not directly available in Rules UI for comparison. Again we have a module Token Var which exposes configured variables as token and these tokens can now be used for comparison via Rules UI and in action we can define our send mail action.

Alternative to Token Var: Rules Condition Site Variable

Tags

  • Drupal
  • Log in to post comments

Comments

Explore topics

TwigQED42Acquia CertificationDrupal PlanetDrupal CertificationThinkingDrupalOpen SourceLifeBackend Specialist
more tags

Quick Notes

Accessing Translated Entity in Views Twig | Drupal 8
Making a variable available (as global) in all twig files in Drupal
Data Structures and Algorithms - Solved in Javascript | Geeksforgeeks
Create alias for frequently used commands
Acquia Front-End Specialist Exam Preparation Pointers

Recent Stories around me

Software Engineer Interview Experience with Glovo
Why Amazon is better than Flipkart - Indian ecommerce market
Acquia Certified Drupal 8 Grand Master
Seminar at DCRUST on Open Source as Career
A Walk to Remember | June 2015 - August 2018 | QED42

Recent write-ups | PK's pen

How to check if Memcache, opcache or varnish is functional with the Drupal 8 application?
Getting started with ReactJS & Drupal (Fully-Decoupled)
Extending Drupal Translations for Custom Entity in Drupal 7
Google Assistant Integration with Drupal
Art of writing template files - Drupal 8
PK's Diary
Powered by Drupal