You are here

function social_comment_tokens in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  2. 8.2 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  3. 8.3 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  4. 8.4 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  5. 8.5 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  6. 8.6 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  7. 8.7 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  8. 8.8 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  9. 10.3.x modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  10. 10.0.x modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  11. 10.1.x modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  12. 10.2.x modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()

Implements hook_tokens().

File

modules/social_features/social_comment/social_comment.tokens.inc, line 44
Builds placeholder replacement tokens for Social Comment module.

Code

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;
}