You are here

notifications_widget.module in Notifications widget 8

File

notifications_widget.module
View source
<?php

/**
 * @file
 * Contains notifications_widget.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function notifications_widget_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the notifications widget module.
    case 'help.page.notifications_widget':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Notification Module allows you to configure the site notification on bell with feature of :') . '</p>';
      $output .= '<ul><li>' . t('Read') . '</li><li>' . t('Unread') . '</li><li>' . t('Delete') . '</li><li>' . t('Clear All') . '</li></ul>';
      $output .= '<p>' . t('It allows to customize the notification message.It provides a block which contains notifications based on settings') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_page_top().
 */
function notifications_widget_page_top() {

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  $route_match = \Drupal::routeMatch();
  if ($admin_context
    ->isAdminRoute($route_match
    ->getRouteObject()) && \Drupal::currentUser()
    ->hasPermission('administer site configuration')) {
    $route_name = \Drupal::routeMatch()
      ->getRouteName();
    switch ($route_name) {

      // These pages don't need additional nagging.
      case 'update.theme_update':
      case 'system.theme_install':
      case 'update.module_update':
      case 'update.module_install':
      case 'update.status':
      case 'update.report_update':
      case 'update.report_install':
      case 'update.settings':
      case 'system.status':
      case 'update.confirmation_page':
        return;
    }
    $config = \Drupal::config('notifications_widget.settings');
    if (!$config
      ->get('notfication_widget_conf')) {
      \Drupal::messenger()
        ->addMessage(t('Notification widget will work well once you saved the configuration from <a href=":user_settings_url">Notification Widget Settings</a>.', [
        ':user_settings_url' => Url::fromRoute('notifications_widget.notifications_widget_settings')
          ->toString(),
      ]), 'warning');
    }
  }
}

/**
 * Implements hook_theme().
 *
 * Twig template for render notification content.
 */
function notifications_widget_theme($existing, $type, $theme, $path) {
  return [
    'notifications_widget' => [
      'variables' => [
        'uid' => NULL,
        'notification_type' => NULL,
        'total' => NULL,
        'unread' => NULL,
        'notification_list' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_entity_insert().
 */
function notifications_widget_entity_insert(EntityInterface $entity) {

  // Validate bundle name.
  if (!empty($entity
    ->bundle())) {
    $notificationService = \Drupal::service('notifications_widget.logger');
    $notificationConfig = \Drupal::config('notifications_widget.settings');
    $allowedActions = $entity
      ->bundle() . '_enable';
    $parseAllowedActions = explode(',', $notificationConfig
      ->get($allowedActions));
    $messageConfigField = $entity
      ->bundle() . '_noti_create_message';
    $routeConfigField = $entity
      ->bundle() . '_redirect_create_link';
    $configMessage = $notificationConfig
      ->get($messageConfigField);
    $routeLink = $notificationConfig
      ->get($routeConfigField);

    // Prepare message link data.
    $message = [];
    $message['id'] = $entity
      ->id();
    $message['bundle'] = $entity
      ->bundle();
    $message['content'] = $configMessage;
    $message['content_link'] = $routeLink;
    if (in_array('Create', $parseAllowedActions)) {
      $notificationService
        ->logNotification($message, 'create', $entity);
    }
  }
}

/**
 * Implements hook_entity_update().
 */
function notifications_widget_entity_update(EntityInterface $entity) {

  // Validate bundle name.
  if (!empty($entity
    ->bundle())) {
    $notificationService = \Drupal::service('notifications_widget.logger');
    $notificationConfig = \Drupal::config('notifications_widget.settings');
    $allowedActions = $entity
      ->bundle() . '_enable';
    $parseAllowedActions = explode(',', $notificationConfig
      ->get($allowedActions));
    $messageConfigField = $entity
      ->bundle() . '_noti_update_message';
    $routeConfigField = $entity
      ->bundle() . '_redirect_update_link';
    $configMessage = $notificationConfig
      ->get($messageConfigField);
    $routeLink = $notificationConfig
      ->get($routeConfigField);

    // Prepare message link data.
    $message = [];
    $message['id'] = $entity
      ->id();
    $message['bundle'] = $entity
      ->bundle();
    $message['content'] = $configMessage;
    $message['content_link'] = $routeLink;
    if (in_array('Update', $parseAllowedActions)) {
      $notificationService
        ->logNotification($message, 'update', $entity);
    }
  }
}

/**
 * Implements hook_entity_delete().
 */
function notifications_widget_entity_delete(EntityInterface $entity) {

  // Validate bundle name.
  if (!empty($entity
    ->bundle())) {
    $notificationService = \Drupal::service('notifications_widget.logger');
    $notificationConfig = \Drupal::config('notifications_widget.settings');
    $allowedActions = $entity
      ->bundle() . '_enable';
    $parseAllowedActions = explode(',', $notificationConfig
      ->get($allowedActions));
    $messageConfigField = $entity
      ->bundle() . '_noti_delete_message';
    $routeConfigField = $entity
      ->bundle() . '_redirect_delete_link';
    $configMessage = $notificationConfig
      ->get($messageConfigField);
    $routeLink = $notificationConfig
      ->get($routeConfigField);

    // Prepare message link data.
    $message = [];
    $message['id'] = $entity
      ->id();
    $message['bundle'] = $entity
      ->bundle();
    $message['content'] = $configMessage;
    $message['content_link'] = $routeLink;
    if (in_array('Delete', $parseAllowedActions)) {
      $notificationService
        ->logNotification($message, 'delete', $entity);
    }
  }
}

/**
 * Implements hook_page_attachments().
 */
function notifications_widget_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'notifications_widget/drupal.notifications';
}

/**
 * Implements hook_views_data().
 *
 * Adds a relationship from the notification table to its' entities.
 */
function notifications_widget_views_data() {
  $data['notifications']['table']['group'] = t('Notifications');
  $data['notifications']['table']['base'] = [
    // This is the identifier field for the view.
    'field' => 'id',
    'title' => t('Notifications'),
    'help' => t('Contains Notifications content and can be related to entity.'),
  ];
  $data['comment_field_data']['notifications']['relationship'] = [
    'title' => t('Notifications Comment'),
    'label' => t('Notifications with Comment entity'),
    'group' => 'Notification',
    'help' => t('Reference to the notification of a Comment entity.'),
    'id' => 'standard',
    'base' => 'notifications',
    'base field' => 'entity_id',
    'field' => 'cid',
    'extra' => [
      [
        'left_field' => 'comment_type',
        'field' => 'bundle',
      ],
    ],
  ];
  $data['node_field_data']['notifications']['relationship'] = [
    'title' => t('Notifications with entity'),
    'label' => t('Notifications with entity'),
    'group' => 'Notification',
    'help' => t('Reference to the notification of a entity.'),
    'id' => 'standard',
    'base' => 'notifications',
    'base field' => 'entity_id',
    'field' => 'nid',
    'extra' => [
      [
        'left_field' => 'type',
        'field' => 'bundle',
      ],
    ],
  ];
  $data['taxonomy_term_field_data']['notifications']['relationship'] = [
    'title' => t('Notifications with term'),
    'label' => t('Notifications with term'),
    'group' => 'Notification',
    'help' => t('Reference to the notification of a term.'),
    'id' => 'standard',
    'base' => 'notifications',
    'base field' => 'entity_id',
    'field' => 'tid',
    'extra' => [
      [
        'left_field' => 'vid',
        'field' => 'bundle',
      ],
    ],
  ];
  $data['profile']['notifications']['relationship'] = [
    'title' => t('Notifications with profile'),
    'label' => t('Notifications with profile'),
    'group' => 'Notification',
    'help' => t('Reference to the notification of a profile.'),
    'id' => 'standard',
    'base' => 'notifications',
    'base field' => 'entity_id',
    'field' => 'profile_id',
    'extra' => [
      [
        'left_field' => 'type',
        'field' => 'bundle',
      ],
    ],
  ];
  $data['message']['notifications']['relationship'] = [
    'title' => t('Notifications with message'),
    'label' => t('Notifications with message'),
    'group' => 'Notification',
    'help' => t('Reference to the notification of a message.'),
    'id' => 'standard',
    'base' => 'notifications',
    'base field' => 'entity_id',
    'field' => 'mid',
    'extra' => [
      [
        'left_field' => 'template',
        'field' => 'bundle',
      ],
    ],
  ];
  $data['notifications']['user_name'] = [
    'title' => t('Notification User name'),
    'help' => t('Display name of notification user.'),
    'field' => [
      'id' => 'markup',
      'format' => 'full_html',
    ],
    'filter' => [
      'id' => 'string',
    ],
  ];
  $data['notifications']['bundle'] = [
    'title' => t('Notification entity\'s bundles'),
    'help' => t('Display name of entity\'s bundles.'),
    'field' => [
      'id' => 'markup',
      'format' => 'full_html',
    ],
    'filter' => [
      'id' => 'string',
    ],
  ];
  $data['notifications']['status'] = [
    'title' => t('Notification Status'),
    'help' => t('Display status of Notification.'),
    'field' => [
      'id' => 'numeric',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
  ];
  $data['notifications']['id'] = [
    'title' => t('Notification ID'),
    'help' => t('Display ID of Notification.'),
    'field' => [
      'id' => 'numeric',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
  ];
  $data['notifications']['message'] = [
    'title' => t('Notification Message'),
    'help' => t('Display notification message.'),
    'field' => [
      'id' => 'markup',
      'format' => 'full_html',
    ],
    'filter' => [
      'id' => 'string',
    ],
  ];
  $data['notifications']['entity_id'] = [
    'title' => t('Notification entity ID'),
    'help' => t('Display entity id of notificaion.'),
    'field' => [
      'id' => 'numeric',
    ],
    'sort' => [
      'id' => 'standard',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
    'argument' => [
      'id' => 'numeric',
    ],
  ];
  $data['notifications']['uid'] = [
    'title' => t('Notification Author ID'),
    'help' => t('Display author name of the notificaion.'),
    'field' => [
      'id' => 'numeric',
    ],
    'sort' => [
      'id' => 'standard',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
    'argument' => [
      'id' => 'numeric',
    ],
  ];
  $data['notifications']['created'] = [
    'title' => t('Notification Created Date'),
    'help' => t('Display notificaion created date.'),
    'field' => [
      'id' => 'date',
    ],
    'sort' => [
      'id' => 'date',
    ],
    'filter' => [
      'id' => 'date',
    ],
  ];
  return $data;
}