Skip to content

Instantly share code, notes, and snippets.

@ericjgruber
Created April 23, 2019 17:21
Show Gist options
  • Save ericjgruber/ddb1cea28d50b433a7822a17f4ba5cf6 to your computer and use it in GitHub Desktop.
Save ericjgruber/ddb1cea28d50b433a7822a17f4ba5cf6 to your computer and use it in GitHub Desktop.
Send an email when a Drupal 8 entity has been updated.
<?php
use Drupal\Core\Entity\EntityInterface;
function HOOK_entity_update(EntityInterface $entity) {
$host = 'local';
if (array_key_exists('AH_SITE_ENVIRONMENT', $_ENV)) {
$host = $_ENV['AH_SITE_ENVIRONMENT'];
}
elseif (array_key_exists('AH_SITE_ENVIRONMENT', $_SERVER)) {
$host = $_SERVER['AH_SITE_ENVIRONMENT'];
}
// Check whether current environment is a dev environment based on $host.
$is_dev = (in_array($host, ['local', 'dev', 'test']));
$params = [];
$vendor_email = \Drupal::config('mailer.settings')->get('admin_email');
$user = User::load(\Drupal::currentUser()->id());
$i = 0;
if ($entity->getEntityTypeId() == 'node' && $entity->getType() == 'article' && $user->hasRole('user')) {
$params[$i]['key'] = 'user-updates-article';
$params[$i]['email_to'] = $vendor_email;
$params[$i]['template_name'] = 'partner-updates-city.html.twig';
$params[$i]['subject'] = 'An Article Update Has Been Made - ' . $entity->get('title')->value;
$params[$i]['reply_email'] = \Drupal::currentUser()->getEmail();
$params[$i]['copy_vendor_email'] = FALSE;
$params[$i]['vars'] = [
'title' => $entity->get('title')->value,
'nid' => $entity->id(),
'url' => $entity->toUrl()->toString(),
'user_email' => $user->get('mail')->value,
'is_dev' => $is_dev,
];
$i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment