You are here

function activity_creator_tokens in Open Social 8.2

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

Implements hook_tokens().

File

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

Code

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

    /** @var \Drupal\message\Entity\Message $message */
    $message = $data['message'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'field_activity_recipient_user_display_name':

          // Get the targeted user, and his display name.
          if ($name === 'field_activity_recipient_user_display_name') {
            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);

              // Or special handling for post entities.
              if ($entity
                ->getEntityTypeId() === 'post') {
                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);
                  if (!empty($recipient_user)) {

                    /** @var \Drupal\user\Entity\User $recipient_user_entity */
                    $replacements[$original] = $recipient_user
                      ->getDisplayName();
                  }
                }
              }
            }
          }
          break;
      }
    }
  }
  return $replacements;
}