You are here

function activity_logger_tokens in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  2. 8.2 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  3. 8.3 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  4. 8.4 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  5. 8.5 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  6. 8.6 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  7. 8.7 modules/custom/activity_logger/activity_logger.tokens.inc \activity_logger_tokens()
  8. 8.8 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 57
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'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'gtitle':
        case 'gurl':
        case 'recipient-user-url':
        case 'pmt-url':
          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;
            $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();
              }

              // 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();
              }

              // 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') {
                  $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-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();
              }
            }
          }
          break;
      }
    }
  }
  return $replacements;
}