Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_mail().
- */
- function ipnotification_mail($key, &$message, $params) {
- $options = array(
- 'langcode' => $message['langcode'],
- );
- switch ($key) {
- case 'entity_create':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = $this->t('%entity created', ['%entity' => $params['title']], $options);
- $message['body'][] = Html::escape($params['message']);
- break;
- case 'entity_delete':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = $this->t('%entity deleted', ['%entity' => $params['title']], $options);
- $message['body'][] = $params['message'];
- break;
- case 'entity_update':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = $this->t('%entity updated', ['%entity' => $params['title']], $options);
- $message['body'][] = $params['message'];
- break;
- case 'user_login':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = $this->t('User has logged in', $options);
- $message['body'][] = $params['message'];
- break;
- case 'user_logout':
- $message['from'] = Drupal::config('system.site')->get('mail');
- $message['subject'] = $this->t('User has logged out', $options);
- $message['body'][] = $params['message'];
- break;
- }
- }
- /**
- * Send email to site admin.
- *
- * @param string $key
- * The mail key to check on 5 available entity_create, entity_delete, entity_update, user_login, user_logout.
- * @param string $entity_label
- * The label of the current entity.
- * @param string $link
- * The link where stuff happened.
- */
- public function sendMail($key, $entity_label, $link) {
- $module = 'ipnotfiication';
- $to = Drupal::config('system.site')->get('mail');
- $params['title'] = $entity_label;
- $params['user'] = $this->user->getAccountName();
- $params['date'] = date('Y-m-d H:i:s');
- $params['mail'] = $this->user->getEmail();
- $params['link'] = $link;
- if (strpos('entity', $key)) {
- if (strpos('create', $key)) {
- $params['message'] = $this->t('User %user created Entity %link: %title',
- array('%title' => $params['title'], '%link' => $params['link']));
- }
- if (strpos('delete', $key)) {
- $params['message'] = $this->t('User %user deleted Entity %link: %title',
- array('%title' => $params['title'], '%link' => $params['link']));
- }
- if (strpos('update', $key)) {
- $params['message'] = $this->t('User %user updated Entity %link: %title',
- array('%title' => $params['title'], '%link' => $params['link']));
- }
- }
- else {
- if (strpos('login', $key)) {
- $params['message'] = $this->t('User %user has logged in on %date ',
- array(
- '%user' => $params['user'],
- '%date' => $params['date'],
- ));
- }
- else {
- $params['message'] = $this->t('User %user has logged out on %date ',
- array(
- '%user' => $params['user'],
- '%date' => $params['date'],
- ));
- }
- }
- $langcode = $this->user->getPreferredLangcode();
- $send = true;
- $result = $this->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