You are here

public function ActivityLoggerFactory::createMessages in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  2. 8 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  3. 8.2 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  4. 8.3 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  5. 8.5 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  6. 8.6 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  7. 8.7 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  8. 8.8 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  9. 10.3.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  10. 10.0.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  11. 10.1.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
  12. 10.2.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()

Create message entities.

Parameters

\Drupal\Core\Entity\Entity $entity: Entity object to create a message for.

string $action: Action string. Defaults to 'create'.

File

modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php, line 24

Class

ActivityLoggerFactory
Class ActivityLoggerFactory.

Namespace

Drupal\activity_logger\Service

Code

public function createMessages(Entity $entity, $action) {

  // Get all messages that are responsible for creating items.
  $message_types = $this
    ->getMessageTypes($action, $entity);

  // Loop through those message types and create messages.
  foreach ($message_types as $message_type => $message_values) {

    // Create the ones applicable for this bundle.
    // Determine destinations.
    $destinations = [];
    if (!empty($message_values['destinations']) && is_array($message_values['destinations'])) {
      foreach ($message_values['destinations'] as $destination) {
        $destinations[] = [
          'value' => $destination,
        ];
      }
    }
    $mt_context = $message_values['context'];

    // Set the values.
    $new_message['template'] = $message_type;
    $new_message['created'] = $entity
      ->getCreatedTime();

    // Get the owner or default to anonymous.
    if (method_exists($entity, 'getOwner') && $entity
      ->getOwner() !== NULL) {
      $new_message['uid'] = $entity
        ->getOwner()
        ->id();
    }
    else {
      $new_message['uid'] = 0;
    }
    $additional_fields = [
      [
        'name' => 'field_message_context',
        'type' => 'list_string',
      ],
      [
        'name' => 'field_message_destination',
        'type' => 'list_string',
      ],
      [
        'name' => 'field_message_related_object',
        'type' => 'dynamic_entity_reference',
      ],
    ];
    $this
      ->createFieldInstances($message_type, $additional_fields);
    $new_message['field_message_context'] = $mt_context;
    $new_message['field_message_destination'] = $destinations;
    $new_message['field_message_related_object'] = [
      'target_type' => $entity
        ->getEntityTypeId(),
      'target_id' => $entity
        ->id(),
    ];

    // Create the message only if it doesn't exist.
    if (!$this
      ->checkIfMessageExist($new_message['template'], $new_message['field_message_context'], $new_message['field_message_destination'], $new_message['field_message_related_object'], $new_message['uid'])) {
      $message = Message::create($new_message);
      $message
        ->save();
    }
  }
}