admin_content_notification.module in Admin Content Notification 8
Same filename and directory in other branches
Module File, consist all related hooks.
File
admin_content_notification.moduleView source
<?php
/**
* @file
* Module File, consist all related hooks.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function admin_content_notification_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.admin_content_notification':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module can be used to send email to admin or on any specific email id when a content has been posted/updated on Drupal site.') . '</p>';
return $output;
}
}
/**
* Implements hook_node_insert().
*/
function admin_content_notification_node_insert(EntityInterface $node) {
if (admin_content_notification_is_current_user_role_allowed_to_send_notification()) {
admin_content_notification_send_email($node, TRUE);
}
}
/**
* Implements hook_node_update().
*/
function admin_content_notification_node_update(EntityInterface $node) {
if (!empty(\Drupal::config('admin_content_notification.settings')
->get('admin_content_notification_trigger_on_node_update')) && admin_content_notification_is_current_user_role_allowed_to_send_notification()) {
admin_content_notification_send_email($node);
}
}
/**
* Check if current user allowed to send admin content notification.
*
* @return bool
* Return true if allowed to send admin content notification.
*/
function admin_content_notification_is_current_user_role_allowed_to_send_notification() {
$roles = \Drupal::currentUser()
->getRoles();
$trigger_for_roles = \Drupal::config('admin_content_notification.settings')
->get('admin_content_notification_allowed_roles') ?: array();
return count(array_intersect(array_filter($trigger_for_roles), $roles));
}
/**
* Send out the admin_content_notification.
*
* @param \Drupal\Core\Entity\EntityInterface $node
*/
function admin_content_notification_send_email(EntityInterface $node, $is_new = FALSE) {
global $base_url;
$node_type = $node
->getType();
// Checking if the nodetype is the one selected.
$selected_node_types = \Drupal::config('admin_content_notification.settings')
->get('admin_content_notification_node_types');
if (count($selected_node_types) && in_array($node_type, $selected_node_types)) {
$user_id = $node
->getOwner();
$user_name = $user_id
->getUsername();
$url = Url::fromUri($base_url . '/node/' . $node
->id());
$internal_link = \Drupal::l(t('%title', array(
'%title' => $node
->label(),
)), $url);
$variables = array(
'%user_who_posted' => $user_name,
'%content_link' => $internal_link,
'%content_title' => $node
->label(),
'%content_type' => $node_type,
'%action' => $is_new ? t('posted') : t('updated'),
);
$config = Drupal::config('admin_content_notification.settings');
$subject = t($config
->get('admin_content_notification_email_subject'), $variables);
$body = t($config
->get('admin_content_notification_email_body'), $variables);
$admin_email = $config
->get('admin_content_notification_email');
$params = array(
'body' => $body,
'subject' => $subject,
);
$key = 'admin_content_notification_key';
\Drupal::service('plugin.manager.mail')
->mail('admin_content_notification', $key, $admin_email, 'en', $params, TRUE);
}
}
/**
* Implements hook_mail().
*/
function admin_content_notification_mail($key, &$message, $params) {
switch ($key) {
case 'admin_content_notification_key':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
Functions
Name | Description |
---|---|
admin_content_notification_help | Implements hook_help(). |
admin_content_notification_is_current_user_role_allowed_to_send_notification | Check if current user allowed to send admin content notification. |
admin_content_notification_mail | Implements hook_mail(). |
admin_content_notification_node_insert | Implements hook_node_insert(). |
admin_content_notification_node_update | Implements hook_node_update(). |
admin_content_notification_send_email | Send out the admin_content_notification. |