activity_creator.tokens.inc in Open Social 8.2
Same filename and directory in other branches
- 8.9 modules/custom/activity_creator/activity_creator.tokens.inc
- 8 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.3 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.4 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.5 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.6 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.7 modules/custom/activity_creator/activity_creator.tokens.inc
- 8.8 modules/custom/activity_creator/activity_creator.tokens.inc
- 10.3.x modules/custom/activity_creator/activity_creator.tokens.inc
- 10.0.x modules/custom/activity_creator/activity_creator.tokens.inc
- 10.1.x modules/custom/activity_creator/activity_creator.tokens.inc
- 10.2.x modules/custom/activity_creator/activity_creator.tokens.inc
Builds placeholder replacement tokens for message-related data.
File
modules/custom/activity_creator/activity_creator.tokens.incView source
<?php
/**
* @file
* Builds placeholder replacement tokens for message-related data.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function activity_creator_token_info() {
$type = [
'name' => t('Activity Entity tokens'),
'description' => t('Tokens from the activity creator module.'),
'needs-data' => 'activity',
];
$activity['field_activity_recipient_user_display_name'] = [
'name' => t("Recipient user entity display name"),
'description' => t("The recipient user entity display name."),
];
return [
'types' => [
'activity' => $type,
],
'tokens' => [
'activity' => $activity,
],
];
}
/**
* Implements hook_tokens().
*/
function activity_creator_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'activity' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'field_activity_recipient_user_display_name':
// Get the targeted user, and his display name.
if ($name === 'field_activity_recipient_user_display_name') {
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);
// Or special handling for post entities.
if ($entity
->getEntityTypeId() === 'post') {
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);
if (!empty($recipient_user)) {
/** @var \Drupal\user\Entity\User $recipient_user_entity */
$replacements[$original] = $recipient_user
->getDisplayName();
}
}
}
}
}
break;
}
}
}
return $replacements;
}
Functions
Name | Description |
---|---|
activity_creator_tokens | Implements hook_tokens(). |
activity_creator_token_info | Implements hook_token_info(). |