social_comment.tokens.inc in Open Social 8.2
Same filename and directory in other branches
- 8.9 modules/social_features/social_comment/social_comment.tokens.inc
- 8 modules/social_features/social_comment/social_comment.tokens.inc
- 8.3 modules/social_features/social_comment/social_comment.tokens.inc
- 8.4 modules/social_features/social_comment/social_comment.tokens.inc
- 8.5 modules/social_features/social_comment/social_comment.tokens.inc
- 8.6 modules/social_features/social_comment/social_comment.tokens.inc
- 8.7 modules/social_features/social_comment/social_comment.tokens.inc
- 8.8 modules/social_features/social_comment/social_comment.tokens.inc
- 10.3.x modules/social_features/social_comment/social_comment.tokens.inc
- 10.0.x modules/social_features/social_comment/social_comment.tokens.inc
- 10.1.x modules/social_features/social_comment/social_comment.tokens.inc
- 10.2.x modules/social_features/social_comment/social_comment.tokens.inc
Builds placeholder replacement tokens for Social Comment module.
File
modules/social_features/social_comment/social_comment.tokens.incView source
<?php
/**
* @file
* Builds placeholder replacement tokens for Social Comment module.
*/
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\NodeInterface;
use Drupal\social_post\Entity\PostInterface;
/**
* Implements hook_token_info().
*/
function social_comment_token_info() {
$type = [
'name' => t('Social Comment'),
'description' => t('Tokens from the social comment module.'),
];
$social_comment['parent_entity_author'] = [
'name' => t('The author of the parent entity.'),
'description' => t('The author of the content type that was commented on.'),
];
$social_comment['commented_content_type'] = [
'name' => t('The commented content type.'),
'description' => t('The type of the content that was commented on.'),
];
return [
'types' => [
'social_comment' => $type,
],
'tokens' => [
'social_comment' => $social_comment,
],
];
}
/**
* Implements hook_tokens().
*/
function social_comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'social_comment' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'parent_entity_author':
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;
$comment = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
// Or special handling for post entities. \Drupal::logger('commented_content_type')->notice(var_dump($comment));
if (!empty($comment)) {
if ($comment
->getEntityTypeId() == 'comment') {
if (!empty($comment
->getCommentedEntity())) {
$node = $comment
->getCommentedEntity();
$owner = $node
->getOwner();
$name = $owner
->getDisplayName();
if (!empty($name)) {
$replacements[$original] = $name;
}
}
}
}
}
break;
case 'commented_content_type':
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\comment\Entity\Comment $comment */
$comment = \Drupal::entityTypeManager()
->getStorage($target_type)
->load($target_id);
if (!empty($comment) && $comment
->getEntityTypeId() == 'comment') {
/** @var \Drupal\Core\Entity\Entity $entity */
$entity = $comment
->getCommentedEntity();
if (!empty($entity)) {
if ($entity instanceof PostInterface) {
$display_name = Unicode::strtolower($entity
->getEntityType()
->getLabel());
}
elseif (is_callable([
$entity,
'getDisplayName',
])) {
$display_name = $entity
->getDisplayName();
}
else {
if ($entity instanceof NodeInterface) {
$display_name = strtolower($entity->type->entity
->label());
}
else {
$display_name = $entity
->bundle();
}
}
if (!empty($display_name)) {
$replacements[$original] = $display_name;
}
}
}
}
break;
}
}
}
return $replacements;
}
Functions
Name | Description |
---|---|
social_comment_tokens | Implements hook_tokens(). |
social_comment_token_info | Implements hook_token_info(). |