View source
<?php
use Drupal\message\Entity\Message;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
use Drupal\activity_creator\Entity\Activity;
const ACTIVITY_STATUS_RECEIVED = 1;
const ACTIVITY_STATUS_SEEN = 2;
const ACTIVITY_STATUS_READ = 3;
function activity_creator_field_activity_status_allowed_values() {
$allowed_values = [
ACTIVITY_STATUS_RECEIVED => 'Received',
ACTIVITY_STATUS_SEEN => 'Seen',
ACTIVITY_STATUS_READ => 'Read',
];
return $allowed_values;
}
function activity_creator_field_activity_destinations_allowed_values() {
$allowed_values = [
'stream_profile' => 'Stream (profile)',
'stream_home' => 'Stream (home)',
'stream_group' => 'Stream (group)',
'stream_explore' => 'Stream (explore)',
'notifications' => 'Notifications',
];
return $allowed_values;
}
function activity_creator_message_insert(Message $entity) {
if ($entity) {
$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');
$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) {
$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);
}
}
}
function activity_creator_views_data() {
$data['activity']['activity_visibility_access_filter'] = [
'title' => t('Activity visibility access'),
'filter' => [
'title' => t('Activity visibility access'),
'help' => t('Enable activity access control.'),
'field' => 'field_visibility',
'id' => 'activity_visibility_access',
],
];
$data['activity']['activity_post_visibility_access_filter'] = [
'title' => t('Activity post visibility access'),
'filter' => [
'title' => t('Activity post visibility access'),
'help' => t('Enable activity access control for related post entities.'),
'field' => 'field_visibility',
'id' => 'activity_post_visibility_access',
],
];
$data['activity']['activity_profile_argument'] = [
'title' => t('Activity profile argument'),
'argument' => [
'title' => t('Activity profile argument'),
'help' => t('Activity profile context argument.'),
'id' => 'activity_profile_argument',
],
];
$data['activity']['activity_group_argument'] = [
'title' => t('Activity group argument'),
'argument' => [
'title' => t('Activity group argument'),
'help' => t('Activity group context argument.'),
'id' => 'activity_group_argument',
],
];
return $data;
}
function activity_creator_theme() {
$items = [
'activity' => [
'render element' => 'elements',
'file' => 'activity.page.inc',
'template' => 'activity',
],
];
return $items;
}
function activity_creator_theme_suggestions_activity(array $variables) {
$suggestions = [];
$activity = $variables['elements']['#activity'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'activity__' . $sanitized_view_mode;
$suggestions[] = 'activity__' . $activity
->id();
$suggestions[] = 'activity__' . $activity
->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
function activity_creator_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($view_mode === 'full') {
$account = \Drupal::currentUser();
\Drupal::service('activity_creator.activity_notifications')
->markEntityNotificationsAsRead($account, $entity);
}
if ($entity instanceof Activity && !empty($entity
->get('field_activity_message')->target_id)) {
$message = Message::load($entity
->get('field_activity_message')->target_id);
if ($message instanceof Message) {
$output = \Drupal::service('activity_creator.activity_factory')
->getMessageText($message);
$build['field_activity_output_text'][0]['#text'] = $output[0];
}
}
}
function activity_creator_preprocess_views_view_unformatted(&$variables) {
$view = $variables['view'];
$view_name = $view->storage
->id();
if ($view_name == 'activity_stream_notifications') {
$notification_url = Url::fromRoute('view.activity_stream_notifications.page_1');
$variables['notification_url'] = $notification_url;
}
}
function activity_creator_entity_delete(EntityInterface $entity) {
if ($entity !== NULL) {
$entity_type = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
if ($entity_type !== 'activity') {
$entity_query = \Drupal::entityQuery('activity');
$entity_query
->condition('field_activity_entity.target_id', $entity_id, '=');
$entity_query
->condition('field_activity_entity.target_type', $entity_type, '=');
$ids = $entity_query
->execute();
if (!empty($ids)) {
entity_delete_multiple('activity', $ids);
}
}
if ($entity_type == 'user' || $entity_type == 'group') {
$entity_query = \Drupal::entityQuery('activity');
$entity_query
->condition('field_activity_recipient_' . $entity_type, $entity_id, '=');
$ids = $entity_query
->execute();
if (!empty($ids)) {
entity_delete_multiple('activity', $ids);
}
}
if ($entity_type == 'group_content') {
$group_content = $entity;
$linked_entity = $group_content
->getEntity();
$group = $group_content
->getGroup();
if ($linked_entity && $linked_entity
->getEntityTypeId() === 'node' && $group
->id()) {
$entity_query = \Drupal::entityQuery('activity');
$entity_query
->condition('field_activity_entity.target_id', $linked_entity
->id(), '=');
$entity_query
->condition('field_activity_entity.target_type', $linked_entity
->getEntityTypeId(), '=');
$entity_query
->condition('field_activity_recipient_group', $group
->id(), '=');
$ids = $entity_query
->execute();
if (!empty($ids)) {
entity_delete_multiple('activity', $ids);
}
}
}
}
}
function activity_creator_cron() {
activity_creator_empty_queue();
}
function activity_creator_empty_queue() {
$queues = [
'activity_creator_activities',
'activity_creator_logger',
'activity_logger_message',
'activity_send_email_worker',
];
$workerManager = \Drupal::service('plugin.manager.queue_worker');
$queue = \Drupal::service('queue');
for ($i = 0; $i < 5; $i++) {
foreach ($workerManager
->getDefinitions() as $name => $info) {
if (in_array($name, $queues)) {
$worker = $queue
->get($name);
$queue_worker = $workerManager
->createInstance($name);
if ($worker
->numberOfItems() > 0) {
while ($item = $worker
->claimItem()) {
$queue_worker
->processItem($item->data);
$worker
->deleteItem($item);
}
}
}
}
}
}