View source
<?php
namespace Drupal\activity_logger\Service;
use Drupal\activity_creator\Plugin\ActivityContextManager;
use Drupal\activity_creator\Plugin\ActivityEntityConditionManager;
use Drupal\Core\Entity\EntityBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\message\Entity\Message;
use Drupal\user\EntityOwnerInterface;
class ActivityLoggerFactory {
protected $entityTypeManager;
protected $activityEntityConditionManager;
protected $activityContextManager;
protected $moduleHandler;
public function __construct(EntityTypeManagerInterface $entityTypeManager, ActivityEntityConditionManager $activityEntityConditionManager, ActivityContextManager $activityContextManager, ModuleHandlerInterface $moduleHandler) {
$this->entityTypeManager = $entityTypeManager;
$this->activityEntityConditionManager = $activityEntityConditionManager;
$this->activityContextManager = $activityContextManager;
$this->moduleHandler = $moduleHandler;
}
public function createMessages(EntityBase $entity, $action) {
$message_types = $this
->getMessageTypes($action, $entity);
foreach ($message_types as $message_type => $message_values) {
$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'];
$new_message['template'] = $message_type;
if ($entity
->getEntityTypeId() === 'flagging') {
$new_message['created'] = $entity
->get('created')->value;
}
else {
$new_message['created'] = $entity
->getCreatedTime();
}
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(),
];
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();
}
}
}
public function getMessageTypes($action, EntityBase $entity) {
$messagetypes = [];
$message_storage = $this->entityTypeManager
->getStorage('message_template');
foreach ($message_storage
->loadByProperties([
'status' => '1',
]) as $key => $messagetype) {
$mt_entity_bundles = $messagetype
->getThirdPartySetting('activity_logger', 'activity_bundle_entities', NULL);
$mt_action = $messagetype
->getThirdPartySetting('activity_logger', 'activity_action', NULL);
$mt_context = $messagetype
->getThirdPartySetting('activity_logger', 'activity_context', NULL);
$mt_destinations = $messagetype
->getThirdPartySetting('activity_logger', 'activity_destinations', NULL);
$mt_entity_condition = $messagetype
->getThirdPartySetting('activity_logger', 'activity_entity_condition', NULL);
if (!empty($mt_entity_condition)) {
$entity_condition_plugin = $this->activityEntityConditionManager
->createInstance($mt_entity_condition);
$entity_condition = $entity_condition_plugin
->isValidEntityCondition($entity);
}
else {
$entity_condition = TRUE;
}
$context_plugin = $this->activityContextManager
->createInstance($mt_context);
$entity_bundle_name = $entity
->getEntityTypeId() . '-' . $entity
->bundle();
if (in_array($entity_bundle_name, $mt_entity_bundles) && $context_plugin
->isValidEntity($entity) && $entity_condition && $action === $mt_action) {
$messagetypes[$key] = [
'messagetype' => $messagetype,
'bundle' => $entity_bundle_name,
'destinations' => $mt_destinations,
'context' => $mt_context,
];
}
}
return $messagetypes;
}
protected function createFieldInstances($message_type, array $fields) {
foreach ($fields as $field) {
$id = 'message.' . $message_type . '.' . $field['name'];
$config_storage = $this->entityTypeManager
->getStorage('field_config');
if ($config_storage
->load($id) === NULL) {
$field_instance = [
'langcode' => 'en',
'status' => TRUE,
'config' => [
'field.storage.message.' . $field['name'],
'message.template.' . $message_type,
],
'module' => [
'options',
],
'id' => $id,
'field_name' => $field['name'],
'entity_type' => 'message',
'bundle' => $message_type,
'label' => '',
'description' => '',
'required' => FALSE,
'translatable' => FALSE,
'default_value' => [],
'default_value_callback' => '',
'field_type' => $field['type'],
];
if ($field['type'] === 'list_string') {
$field_instance['module'] = [
'options',
];
$field_instance['settings'] = [];
}
elseif ($field['type'] === 'dynamic_entity_reference') {
$field_instance['module'] = [
'dynamic_entity_reference',
];
$field_instance['settings'] = [];
}
$config_storage
->create($field_instance)
->save();
}
}
}
public function checkIfMessageExist($message_type, $context, array $destination, array $related_object, $uid) {
$exists = FALSE;
$query = $this->entityTypeManager
->getStorage('message')
->getQuery();
$query
->condition('template', $message_type);
$query
->condition('field_message_related_object.target_id', $related_object['target_id']);
$query
->condition('field_message_related_object.target_type', $related_object['target_type']);
$query
->condition('field_message_context', $context);
$query
->condition('uid', $uid);
if (is_array($destination)) {
foreach ($destination as $delta => $dest_value) {
$query
->condition('field_message_destination.' . $delta . '.value', $dest_value['value']);
}
}
$query
->accessCheck(FALSE);
$types = [
'moved_content_between_groups',
'create_topic_group',
'create_event_group',
];
if (in_array($message_type, $types, TRUE)) {
$query = $this->entityTypeManager
->getStorage('message')
->getQuery();
$query
->condition('template', $types, 'IN');
$query
->condition('field_message_related_object.target_id', $related_object['target_id']);
$query
->condition('field_message_related_object.target_type', $related_object['target_type']);
$query
->condition('field_message_context', $context);
$query
->condition('uid', $uid);
$query
->accessCheck(FALSE);
}
$ids = $query
->execute();
$allowed_duplicates = [
'moved_content_between_groups',
];
$this->moduleHandler
->alter('activity_allowed_duplicates', $allowed_duplicates);
if (!empty($ids) && !in_array($message_type, $allowed_duplicates)) {
$exists = TRUE;
}
return $exists;
}
}