function social_mentions_tokens in Open Social 8.3
Same name and namespace in other branches
- 8.9 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.2 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.4 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.5 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.6 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.7 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 8.8 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 10.3.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 10.0.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 10.1.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
- 10.2.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
Implements hook_tokens().
File
- modules/
social_features/ social_mentions/ social_mentions.tokens.inc, line 49 - Builds placeholder replacement tokens for Social Mentions module.
Code
function social_mentions_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'social_mentions' && !empty($data['profile'])) {
/** @var \Drupal\profile\Entity\Profile $profile */
$profile = $data['profile'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'user_name':
$config = \Drupal::config('mentions.settings');
switch ($config
->get('suggestions_format')) {
case SOCIAL_PROFILE_SUGGESTIONS_FULL_NAME:
case SOCIAL_PROFILE_SUGGESTIONS_ALL:
$user_name = $profile
->getOwner()
->getDisplayName();
}
if (empty($user_name)) {
$user_name = $profile
->getOwner()
->getAccountName();
}
$replacements[$original] = $user_name;
break;
}
}
}
if ($type == 'social_mentions' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'mentioned_user':
if ($name === 'mentioned_user') {
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;
$mention = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if ($mention
->getEntityTypeId() == 'mentions') {
$loadUserId = User::load($mention
->getMentionedUserID());
$user = $loadUserId
->getDisplayName();
$replacements[$original] = $user;
}
}
}
break;
case 'commented_entity_link_html':
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;
/** @var \Drupal\mentions\Entity\Mentions $mention */
$mention = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if ($mentioned_entity = $mention
->getMentionedEntity()) {
if ($mentioned_entity
->getEntityTypeId() === 'comment') {
$entity = $mentioned_entity
->getCommentedEntity();
}
else {
$entity = $mentioned_entity;
}
}
}
if (isset($entity)) {
switch ($entity
->getEntityTypeId()) {
case 'node':
// Then get the bundle name.
$content_type_label = Unicode::strtolower(\Drupal::entityTypeManager()
->getStorage('node_type')
->load($entity
->bundle())
->label());
break;
case 'post':
case 'photo':
case 'comment':
$content_type_label = Unicode::strtolower($entity
->getEntityType()
->getLabel());
break;
}
$url_options = [
'absolute' => TRUE,
];
$link = $entity
->toUrl('canonical', $url_options)
->toString();
// We should only use the label of entities who have a label.
if ($link_label = $entity
->label()) {
$entity_link_html = $content_type_label . ' <a href="' . $link . '">' . $link_label . '</a>';
}
else {
$entity_link_html = '<a href="' . $link . '">' . $content_type_label . '</a>';
}
$replacements[$original] = Markup::create($entity_link_html);
}
break;
}
}
}
return $replacements;
}