You are here

function social_group_tokens in Open Social 8.7

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

Implements hook_tokens().

File

modules/social_features/social_group/social_group.tokens.inc, line 49
Builds placeholder replacement tokens for Social Group module.

Code

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

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

          // Get the related entity.
          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)) {
              switch ($target_type) {

                // If it's group content.
                case 'group_content':

                  /** @var \Drupal\group\Entity\GroupContent $entity */
                  $group_content_type = $entity
                    ->getGroupContentType();
                  if (!empty($group_content_type)) {
                    $display_name = $group_content_type
                      ->label();
                    $content_url = Url::fromRoute('entity.node.canonical', [
                      'node' => $entity
                        ->getEntity()
                        ->id(),
                    ], [
                      'absolute' => TRUE,
                    ]);
                  }
                  break;

                // If it's node.
                case 'node':
                  $display_name = $entity
                    ->bundle();
                  break;

                // When it's a post or photo post.
                case 'photo':
                case 'post':
                  $display_name = Unicode::strtolower($entity
                    ->getEntityType()
                    ->getLabel());
                  $content_url = Url::fromRoute('entity.post.canonical', [
                    'post' => $entity
                      ->id(),
                  ], [
                    'absolute' => TRUE,
                  ]);
                  break;
              }

              // When a name of content name starts from a vowel letter then
              // will be added "an" before this name. For example "an
              // event".
              if (isset($display_name)) {
                if (preg_match('/^[aeiou]/', $display_name)) {
                  $display_name = t('an @content_type', [
                    '@content_type' => $display_name,
                  ]);
                }
                else {
                  $display_name = t('a @content_type', [
                    '@content_type' => $display_name,
                  ]);
                }
              }
              if ($name === 'content_url') {
                if (isset($content_url)) {
                  $replacements[$original] = $content_url
                    ->toString();
                }
              }
              elseif ($name === 'content_type') {
                if (isset($display_name)) {
                  $replacements[$original] = $display_name;
                }
              }
              elseif ($name === 'created_entity_link_html') {

                // We should only use the label of entities who have a label.
                if ($link_label = $entity
                  ->label()) {
                  $entity_link_html = $display_name . ' <a href="' . $content_url
                    ->toString() . '">' . $link_label . '</a>';
                }
                else {
                  $entity_link_html = '<a href="' . $content_url
                    ->toString() . '">' . $display_name . '</a>';
                }
                $replacements[$original] = Markup::create($entity_link_html);
              }
            }
          }
          break;
      }
    }
  }
  return $replacements;
}