You are here

function notifications_widget_entity_insert in Notifications widget 8

Implements hook_entity_insert().

File

./notifications_widget.module, line 85
Contains notifications_widget.module.

Code

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);
    }
  }
}