You are here

function activity_creator_message_insert in Open Social 8.2

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  2. 8 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  3. 8.3 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  4. 8.4 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  5. 8.5 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  6. 8.6 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  7. 8.7 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  8. 8.8 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  9. 10.3.x modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  10. 10.0.x modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  11. 10.1.x modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  12. 10.2.x modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()

Implements hook_ENTITY_TYPE_insert().

File

modules/custom/activity_creator/activity_creator.module, line 61
Activity Creator module file.

Code

function activity_creator_message_insert(Message $entity) {
  if ($entity) {

    // TODO Move all this logic to services (also in Queue).
    $mid = $entity
      ->id();
    $message_template_id = $entity
      ->getTemplate()
      ->id();
    $actor = $entity
      ->getOwner()
      ->id();
    $context = $entity
      ->get('field_message_context')
      ->getValue();
    $destinations = $entity
      ->get('field_message_destination')
      ->getValue();
    $related_object = $entity
      ->get('field_message_related_object')
      ->getValue();
    $data = [
      'mid' => $mid,
      'message_template' => $message_template_id,
      'actor' => $actor,
      'context' => $context[0]['value'],
      'destination' => $destinations,
      'related_object' => $related_object,
      'last_uid' => 0,
      'status' => NULL,
    ];
    $message_template = $entity
      ->getTemplate();
    $activity_create_direct = $message_template
      ->getThirdPartySetting('activity_logger', 'activity_create_direct', NULL);
    if ($activity_create_direct) {
      $context_plugin_manager = \Drupal::service('plugin.manager.activity_context.processor');

      /* @var $plugin \Drupal\activity_creator\Plugin\ActivityContextBase */
      $plugin = $context_plugin_manager
        ->createInstance($data['context']);
      $recipients = $plugin
        ->getRecipients($data, $data['last_uid'], 0);
      $activity_factory = \Drupal::service('activity_creator.activity_factory');
      if (!empty($recipients)) {
        foreach ($recipients as $recipient) {

          // Create a queue item for activity creation.
          $activity_creator_data = [
            'mid' => $data['mid'],
            'message_template' => $data['message_template'],
            'actor' => $data['actor'],
            'context' => $data['context'],
            'destination' => $data['destination'],
            'related_object' => $data['related_object'],
            'recipient' => $recipient,
          ];
          $activity_factory
            ->createActivities($activity_creator_data);
        }
      }
      else {
        $activity_creator_data = [
          'mid' => $data['mid'],
          'message_template' => $data['message_template'],
          'actor' => $data['actor'],
          'context' => $data['context'],
          'destination' => $data['destination'],
          'related_object' => $data['related_object'],
        ];
        $activity_factory
          ->createActivities($activity_creator_data);
      }
    }
    else {
      $queue = \Drupal::queue('activity_creator_logger');
      $queue
        ->createItem($data);
    }
  }
}