You are here

function activity_logger_tokens in Open Social 8.8

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

Implements hook_tokens().

File

modules/custom/activity_logger/activity_logger.tokens.inc, line 66
Builds placeholder replacement tokens for message-related data.

Code

function activity_logger_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type === 'message' && !empty($data['message'])) {

    /** @var \Drupal\message\Entity\Message $message */
    $message = $data['message'];
    $token_service = \Drupal::token();
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'node-title':
        case 'gtitle':
        case 'gurl':
        case 'recipient-user':
        case 'recipient-user-url':
        case 'pmt-url':
          if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
            $target_type = $message->field_message_related_object->target_type;
            $target_id = $message->field_message_related_object->target_id;
            $entity = \Drupal::entityTypeManager()
              ->getStorage($target_type)
              ->load($target_id);
            if (is_object($entity)) {

              // If comment get the entity to which the comment is attached.
              if ($entity
                ->getEntityTypeId() === 'comment') {
                $entity = $entity
                  ->getCommentedEntity();

                // It could happen that a notification has been queued but by
                // now the "commented entity" has been deleted.
                if (!$entity instanceof FieldableEntityInterface) {
                  break;
                }
              }

              // When it is a node.
              if ($entity
                ->getEntityTypeId() === 'node') {
                $node = $entity;
              }

              // Try to get the group.
              $group_content = GroupContent::loadByEntity($entity);
              if (!empty($group_content)) {
                $group_content = reset($group_content);
                $group = $group_content
                  ->getGroup();
              }

              // Or special handling for post entities.
              if ($entity
                ->getEntityTypeId() === 'post') {
                if (!empty($entity
                  ->get('field_recipient_group')
                  ->getValue())) {
                  $group = Group::load($entity->field_recipient_group->target_id);
                }
                if (isset($entity->field_recipient_user) && !empty($entity->field_recipient_user->target_id)) {
                  $target_id = $entity->field_recipient_user->target_id;
                  $recipient_user = \Drupal::entityTypeManager()
                    ->getStorage('user')
                    ->load($target_id);
                }
              }

              // Handling for group content entities.
              if ($entity
                ->getEntityTypeId() === 'group_content') {
                $group = $entity
                  ->getGroup();
                $group_content_entity = $entity
                  ->getEntity();
                switch ($group_content_entity
                  ->getEntityTypeId()) {
                  case 'node':
                    $node =& $group_content_entity;
                    break;
                  case 'user':
                    $recipient_user =& $group_content_entity;
                    break;
                }
              }

              // Handling for group entities.
              if ($entity
                ->getEntityTypeId() === 'group') {
                $group = $entity;
              }

              // If it's a group.. add it in the arguments.
              if (isset($group) && $group instanceof Group) {
                if ($name === 'gtitle') {
                  $curr_langcode = \Drupal::languageManager()
                    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
                    ->getId();
                  if ($group
                    ->isTranslatable() && $group
                    ->hasTranslation($curr_langcode)) {
                    $group = $group
                      ->getTranslation($curr_langcode);
                  }
                  $replacements[$original] = $group
                    ->label();
                }
                if ($name === 'gurl') {
                  $gurl = Url::fromRoute('entity.group.canonical', [
                    'group' => $group
                      ->id(),
                  ], [
                    'absolute' => TRUE,
                  ]);
                  $replacements[$original] = $gurl
                    ->toString();
                }
              }
              if ($name === 'recipient-user') {
                if (!empty($recipient_user)) {
                  $account =& $recipient_user;
                }
                else {
                  $account = \Drupal::entityTypeManager()
                    ->getStorage('user')
                    ->load(0);
                }

                /** @var \Drupal\user\UserInterface $account */
                $replacements[$original] = $account
                  ->getDisplayName();
              }
              elseif ($name === 'recipient-user-url') {
                if (!empty($recipient_user)) {
                  $target_stream_url = Url::fromRoute('entity.user.canonical', [
                    'user' => $recipient_user
                      ->id(),
                  ], [
                    'absolute' => TRUE,
                  ]);
                  $replacements[$original] = $target_stream_url
                    ->toString();
                }
              }
              if ($name === 'pmt-url') {

                // Get the related message.
                $target_type = $message->field_message_related_object->target_type;
                $target_id = $message->field_message_related_object->target_id;
                $entity = \Drupal::entityTypeManager()
                  ->getStorage($target_type)
                  ->load($target_id);

                // Use the mapper service to get the ID of the thread.
                $pmService = \Drupal::service('private_message.mapper');
                $thread_id = $pmService
                  ->getThreadIdFromMessage($entity);

                // Build the url.
                $thread_url = Url::fromRoute('entity.private_message_thread.canonical', [
                  'private_message_thread' => $thread_id,
                ], [
                  'absolute' => TRUE,
                ]);
                $replacements[$original] = $thread_url
                  ->toString();
              }
              if ($name === 'node-title') {
                if (isset($node) && $node instanceof Node) {
                  $replacements[$original] = $node
                    ->label();
                }
              }
            }
          }
          break;
      }
    }
    $recipient_user_tokens = $token_service
      ->findWithPrefix($tokens, 'recipient-user');
    if ($recipient_user_tokens && !empty($recipient_user)) {
      $replacements += $token_service
        ->generate('user', $recipient_user_tokens, [
        'user' => $recipient_user,
      ], $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}