You are here

function social_mentions_tokens in Open Social 8.2

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  2. 8 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  3. 8.3 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  4. 8.4 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  5. 8.5 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  6. 8.6 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  7. 8.7 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  8. 8.8 modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  9. 10.3.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  10. 10.0.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  11. 10.1.x modules/social_features/social_mentions/social_mentions.tokens.inc \social_mentions_tokens()
  12. 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 42
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;
      }
    }
  }
  return $replacements;
}