You are here

function social_comment_tokens in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  2. 8 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  3. 8.2 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  4. 8.3 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  5. 8.4 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  6. 8.5 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  7. 8.6 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  8. 8.7 modules/social_features/social_comment/social_comment.tokens.inc \social_comment_tokens()
  9. 8.8 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 87
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':
        case 'parent_comment_author':
        case 'parent_comment_date':
        case 'parent_comment_text':
        case 'comment_author':
        case 'comment_date':
        case 'comment_text':
        case 'comment_reply_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;
            $comment = \Drupal::entityTypeManager()
              ->getStorage($target_type)
              ->load($target_id);
            if ($comment instanceof CommentInterface) {
              if ($comment
                ->getEntityTypeId() === 'comment') {
                $date_formatter = \Drupal::service('date.formatter');
                $date_format = 'social_long_date';
                if ($comment
                  ->hasParentComment()) {
                  $parent_comment = $comment
                    ->getParentComment();
                  if ($name === 'parent_comment_author') {
                    $replacements[$original] = $parent_comment
                      ->getOwner()
                      ->getDisplayName();
                  }
                  if ($name === 'parent_comment_date') {
                    $replacements[$original] = $date_formatter
                      ->format($parent_comment
                      ->getCreatedTime(), $date_format);
                  }
                  if ($name === 'parent_comment_text') {
                    if ($parent_comment
                      ->hasField('field_comment_body') && !$parent_comment->field_comment_body
                      ->isEmpty()) {
                      $replacements[$original] = _social_comment_get_summary($parent_comment->field_comment_body->value);
                    }
                    else {
                      $replacements[$original] = '';
                    }
                  }
                }
                if (!empty($comment
                  ->getCommentedEntity())) {
                  $commented_entity = $comment
                    ->getCommentedEntity();
                  if ($name === 'parent_entity_author') {
                    $replacements[$original] = $commented_entity
                      ->getOwner()
                      ->getDisplayName();
                  }
                }
                if ($name === 'comment_author') {
                  $replacements[$original] = $comment
                    ->getOwner()
                    ->getDisplayName();
                }
                if ($name === 'comment_date') {
                  $replacements[$original] = $date_formatter
                    ->format($comment
                    ->getCreatedTime(), $date_format);
                }
                if ($name === 'comment_text') {
                  if ($comment
                    ->hasField('field_comment_body') && !$comment->field_comment_body
                    ->isEmpty()) {
                    $replacements[$original] = _social_comment_get_summary($comment->field_comment_body->value);
                  }
                  else {
                    $replacements[$original] = '';
                  }
                }
                if ($name === 'comment_reply_link_html') {

                  /** @var \Drupal\Core\Entity\Entity $entity */
                  $entity = $comment
                    ->getCommentedEntity();
                  $commented_entity_type = $commented_entity
                    ->getEntityTypeId();

                  // A reply link for a post should go to the post view.
                  if ($commented_entity_type === 'post') {
                    $replacements[$original] = $entity
                      ->toUrl('canonical', [
                      'absolute' => TRUE,
                    ])
                      ->toString();
                  }
                  else {

                    // For comments however it should go to comment reply page.
                    $replacements[$original] = Url::fromRoute('comment.reply', [
                      'entity_type' => $comment
                        ->getCommentedEntityTypeId(),
                      'entity' => $comment
                        ->getCommentedEntityId(),
                      'field_name' => $comment
                        ->getFieldName(),
                      'pid' => $comment
                        ->id(),
                    ])
                      ->toString();
                  }
                }
              }
            }
          }
          break;
        case 'commented_content_type':
        case 'commented_entity_link':
        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;
            $comment = \Drupal::entityTypeManager()
              ->getStorage($target_type)
              ->load($target_id);
            if ($comment instanceof CommentInterface) {

              /** @var \Drupal\Core\Entity\Entity $entity */
              $entity = $comment
                ->getCommentedEntity();
              if (!empty($entity)) {
                if ($entity instanceof PostInterface) {
                  $commented_content_type = mb_strtolower($entity
                    ->getEntityType()
                    ->getLabel());
                }
                elseif (is_callable([
                  $entity,
                  'getDisplayName',
                ])) {
                  $commented_content_type = $entity
                    ->getDisplayName();
                }
                else {
                  if ($entity instanceof NodeInterface) {
                    $commented_content_type = strtolower($entity->type->entity
                      ->label());
                  }
                  else {
                    $commented_content_type = $entity
                      ->bundle();
                  }
                }
                if ($name === 'commented_content_type') {
                  if (!empty($commented_content_type)) {
                    $replacements[$original] = $commented_content_type;
                  }
                }
                if ($name === 'commented_entity_link') {
                  $replacements[$original] = $entity
                    ->toUrl('canonical', [
                    'absolute' => TRUE,
                  ])
                    ->toString();
                }
                if ($name === 'commented_entity_link_html') {
                  $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 = $commented_content_type . ' <a href="' . $link . '">' . $link_label . '</a>';
                  }
                  else {
                    $entity_link_html = '<a href="' . $link . '">' . $commented_content_type . '</a>';
                  }
                  $replacements[$original] = Markup::create($entity_link_html);
                }
              }
            }
          }
          break;
      }
    }
  }
  return $replacements;
}