activity_logger.tokens.inc in Open Social 8.2
Same filename and directory in other branches
- 8.9 modules/custom/activity_logger/activity_logger.tokens.inc
- 8 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.3 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.4 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.5 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.6 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.7 modules/custom/activity_logger/activity_logger.tokens.inc
- 8.8 modules/custom/activity_logger/activity_logger.tokens.inc
- 10.3.x modules/custom/activity_logger/activity_logger.tokens.inc
- 10.0.x modules/custom/activity_logger/activity_logger.tokens.inc
- 10.1.x modules/custom/activity_logger/activity_logger.tokens.inc
- 10.2.x modules/custom/activity_logger/activity_logger.tokens.inc
Builds placeholder replacement tokens for message-related data.
File
modules/custom/activity_logger/activity_logger.tokens.incView source
<?php
/**
* @file
* Builds placeholder replacement tokens for message-related data.
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupContent;
/**
* Implements hook_token_info().
*/
function activity_logger_token_info() {
$type = [
'name' => t('Activity tokens'),
'description' => t('Tokens from the activity logger module.'),
'needs-data' => 'message',
];
$message['node-title'] = [
'name' => t("Node title"),
'description' => t("The related node title."),
];
$message['gtitle'] = [
'name' => t("Groups title"),
'description' => t("The related group title."),
];
$message['gurl'] = [
'name' => t("Groups url"),
'description' => t("The related group url."),
];
$message['pmt-url'] = [
'name' => t("Private Message Thread url"),
'description' => t("The URL of the private message thread group url."),
];
$message['recipient-user-url'] = [
'name' => t("Recipient user url"),
'description' => t("The recipient user url."),
];
return [
'types' => [
'message' => $type,
],
'tokens' => [
'message' => $message,
],
];
}
/**
* Implements hook_tokens().
*/
function activity_logger_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'message' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'gtitle':
case 'gurl':
case 'recipient-user-url':
case 'pmt-url':
if (isset($message->field_message_related_object)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$entity = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if (is_object($entity)) {
// If comment get the entity to which the comment is attached.
if ($entity
->getEntityTypeId() === 'comment') {
$entity = $entity
->getCommentedEntity();
}
// Try to get the group.
$group_content = GroupContent::loadByEntity($entity);
if (!empty($group_content)) {
$group_content = reset($group_content);
$group = $group_content
->getGroup();
}
// Or special handling for post entities.
if ($entity
->getEntityTypeId() === 'post') {
if (!empty($entity
->get('field_recipient_group')
->getValue())) {
$group = Group::load($entity->field_recipient_group->target_id);
}
if (isset($entity->field_recipient_user) && !empty($entity->field_recipient_user->target_id)) {
$target_id = $entity->field_recipient_user->target_id;
$recipient_user = \Drupal::entityTypeManager()
->getStorage('user')
->load($target_id);
}
}
// Handling for group content entities.
if ($entity
->getEntityTypeId() === 'group_content') {
$group = $entity
->getGroup();
}
// Handling for group entities.
if ($entity
->getEntityTypeId() === 'group') {
$group = $entity;
}
// If it's a group.. add it in the arguments.
if (isset($group) && $group instanceof Group) {
if ($name === 'gtitle') {
$replacements[$original] = $group
->label();
}
if ($name === 'gurl') {
$gurl = Url::fromRoute('entity.group.canonical', [
'group' => $group
->id(),
], [
'absolute' => TRUE,
]);
$replacements[$original] = $gurl
->toString();
}
}
if ($name === 'recipient-user-url') {
if (!empty($recipient_user)) {
$target_stream_url = Url::fromRoute('entity.user.canonical', [
'user' => $recipient_user
->id(),
], [
'absolute' => TRUE,
]);
$replacements[$original] = $target_stream_url
->toString();
}
}
if ($name === 'pmt-url') {
// Get the related message.
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$entity = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
// Use the mapper service to get the ID of the thread.
$pmService = \Drupal::service('private_message.mapper');
$thread_id = $pmService
->getThreadIdFromMessage($entity);
// Build the url.
$thread_url = Url::fromRoute('entity.private_message_thread.canonical', [
'private_message_thread' => $thread_id,
], [
'absolute' => TRUE,
]);
$replacements[$original] = $thread_url
->toString();
}
}
}
break;
}
}
}
return $replacements;
}
Functions
Name | Description |
---|---|
activity_logger_tokens | Implements hook_tokens(). |
activity_logger_token_info | Implements hook_token_info(). |