Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implements hook_mail().
- */
- function <module_name>_mail($key, &$message, $params) {
- $options = array(
- 'langcode' => $message['langcode'],
- );
- switch ($key) {
- case 'create_article':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = t('Article created: @title', array('@title' => $params['node_title']), $options);
- $message['body'][] = $params['message'];
- break;
- }
- }
- /**
- * Implements hook_entity_insert().
- */
- function <module_name>_entity_insert(DrupalCoreEntityEntityInterface $entity) {
- if ($entity->getEntityTypeId() !== 'node' || ($entity->getEntityTypeId() === 'node' && $entity->bundle() !== 'article')) {
- return;
- }
- $mailManager = Drupal::service('plugin.manager.mail');
- $module = ‘<module_name>’;
- $key = 'create_article';
- $to = Drupal::currentUser()->getEmail();
- $params['message'] = $entity->get('body')->value;
- $params['node_title'] = $entity->label();
- $langcode = Drupal::currentUser()->getPreferredLangcode();
- $send = true;
- $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
- if ($result['result'] !== true) {
- drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
- }
- else {
- drupal_set_message(t('Your message has been sent.'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement