public function ActivityLoggerFactory::createMessages in Open Social 8.8
Same name and namespace in other branches
- 8.9 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.2 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.3 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.4 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.5 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.6 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 8.7 modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 10.3.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 10.0.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 10.1.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
- 10.2.x modules/custom/activity_logger/src/Service/ActivityLoggerFactory.php \Drupal\activity_logger\Service\ActivityLoggerFactory::createMessages()
Create message entities.
Parameters
\Drupal\Core\Entity\EntityBase $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 25
Class
- ActivityLoggerFactory
- Class ActivityLoggerFactory.
Namespace
Drupal\activity_logger\ServiceCode
public function createMessages(EntityBase $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;
// The flagging entity does not implement getCreatedTime().
if ($entity
->getEntityTypeId() === 'flagging') {
$new_message['created'] = $entity
->get('created')->value;
}
else {
$new_message['created'] = $entity
->getCreatedTime();
}
// Get the owner or default to anonymous.
if ($entity instanceof EntityOwnerInterface && $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();
}
}
}