You are here

function activity_creator_message_insert in Open Social 10.0.x

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.2 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  4. 8.3 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  5. 8.4 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  6. 8.5 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  7. 8.6 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  8. 8.7 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  9. 8.8 modules/custom/activity_creator/activity_creator.module \activity_creator_message_insert()
  10. 10.3.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 65
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 \Drupal\activity_creator\Plugin\ActivityContextBase $plugin */
      $plugin = $context_plugin_manager
        ->createInstance($data['context']);
      $recipients = $plugin
        ->getRecipients($data, $data['last_uid'], 0);
      $user_recipients = [];
      $activity_factory = \Drupal::service('activity_creator.activity_factory');
      if (!empty($recipients)) {

        // Default activity creator template.
        $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'],
        ];

        // Get all the activity recipient types. Maintain target IDs as key.
        $activity_by_type = array_column($recipients, 'target_type');
        foreach ($activity_by_type as $recipients_key => $target_type) {

          // For all one to one target entity types we create an activity.
          if ($target_type !== 'user') {
            $activity_creator_data['recipient'] = $recipients[$recipients_key];
            $activity_factory
              ->createActivities($activity_creator_data);
          }
          if ($target_type === 'user') {
            $user_recipients[] = $recipients[$recipients_key];
          }
        }

        // When the activity should be created for a one to many user entity
        // we like to group these.
        if (!empty($user_recipients)) {
          $activity_creator_data['recipient'] = $user_recipients;
          $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);
    }
  }
}