In projects, there are multiple trigger points where emails have to be sent. In Drupal 7, we had the option where we could define a template and set up rules to trigger emails based on defined action.
But the rules module is still under active migration from D7 to D8, and we were short of time to complete the project.
So, we decided to create custom config entities for creating email templates. There was a need to have a configuration page where admin can add new templates having the subject field as well as text body. To help the admin user, we had to list out all variables available on the same config page. In our case, since the list of trigger actions was pre-defined, we added them programmatically. These actions are available during the email template creation page.
The following steps show how we have implemented it.
Step 1 - Created a custom config entity ‘email_templates’ for the custom module ‘projectname_email_templates’ using the drupal console.
Steps to Generate a custom module using the drupal console:
drupal generate:module // Welcome to the Drupal module generator Enter the new module name: > projectname_email_templates Enter the module machine name [projectname_email_templates]: > Enter the module Path [modules/custom]: > Enter module description [My Awesome Module]: > Module for email templates Enter package name [Custom]: > Enter Drupal Core version [8.x]: > Do you want to generate a .module file? (yes/no) [yes]: > yes Define module as feature (yes/no) [no]: > no Do you want to add a composer.json file to your module? (yes/no) [yes]: > yes Would you like to add module dependencies? (yes/no) [no]: > no Do you want to generate a unit test class? (yes/no) [yes]: > Do you want to generate a themeable template? (yes/no) [yes]: > yes Do you want proceed with the operation? (yes/no) [yes]: > yes // generate:composer Generated or updated files Generation path: /var/www/html/Drupal_proj/my_proj/web 1 - /modules/custom/projectname_email_templates/projectname_email_templates.info.yml 2 - /modules/custom/projectname_email_templates/projectname_email_templates.module 3 - /modules/custom/projectname_email_templates/tests/src/Functional/LoadTest.php 4 - /modules/custom/projectname_email_templates/projectname_email_templates.module 5 - /modules/custom/projectname_email_templates/templates/projectname-email-templates.html.twig 6 - modules/custom/projectname_email_templates/composer.json Generated lines: 100
Steps to Generate custom config entity using the drupal console:
drupal generate:entity:config // Welcome to the Drupal Config Entity generator Enter the module name [address]: > projectname_email_templates Enter the class of your new config entity [DefaultEntity]: > EmailTemplates Enter the name of your new config entity [email_templates]: > Enter the label of your new config entity [Email templates]: > Enter the base-path for the config entity routes [/admin/structure]: > Generated or updated files Generation path: /var/www/html/VB/horse-auction/web 1 - modules/custom/projectname_email_templates/config/schema/email_templates.schema.yml 2 - modules/custom/projectname_email_templates/projectname_email_templates.links.menu.yml 3 - modules/custom/projectname_email_templates/projectname_email_templates.links.action.yml 4 - modules/custom/projectname_email_templates/src/Entity/EmailTemplatesInterface.php 5 - modules/custom/projectname_email_templates/src/Entity/EmailTemplates.php 6 - modules/custom/projectname_email_templates/src/EmailTemplatesHtmlRouteProvider.php 7 - modules/custom/projectname_email_templates/src/Form/EmailTemplatesForm.php 8 - modules/custom/projectname_email_templates/src/Form/EmailTemplatesDeleteForm.php 9 - modules/custom/projectname_email_templates/src/EmailTemplatesListBuilder.php Generated lines: 269
Step 2 - In email_templates.schema.yml (projectname_email_templates/config/schema): Define the variable and its type.
Step 3 - In EmailTemplates.php (projectname_email_templates/sec/Entity) : Define the template entities like entity_keys, config_prefix, admin_permission, links and email templates variables.
Step 4: In EmailTemplatesForm.php projectname_email_templates/src/Form/) we will be creating the required form elements.
Step 5: In the custom module file ‘projectname_email_templates.module’ define a hook_mail() to handle the various trigger points
Step 6: To send emails based on the trigger point, we are using plugin.manager.mail service.
Step 7: Drupal views do not support displaying custom config entities. So, In order to list all the email templates created, We are using a drupal custom page with a custom HTML table.
We have created a custom page by creating a route in projectname.routing.yml and creating a controller function 'emailTemplateList' in the AdminPagesController.php
Note: If you want to give access to other than Admin users, we need Add, Edit, delete and view config entity. we need to give the permission -Administer site configuration (admin/people/permissions)
Get in touch with us to know more about how Drupal can help your in achieving your unique business requirements.