You are here

public function ShareMessage::buildTwitterCardTags in Share Message 8

Adds meta tags in order to share images on Twitter.

Parameters

array $context: The context for the token replacements.

Return value

array The twitter tags.

Overrides ShareMessageInterface::buildTwitterCardTags

File

src/Entity/ShareMessage.php, line 418

Class

ShareMessage
Entity class for the Share Message entity.

Namespace

Drupal\sharemessage\Entity

Code

public function buildTwitterCardTags($context) {
  $twitter = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'twitter:card',
      'content' => 'summary_large_image',
    ],
  ];
  $tags[] = [
    $twitter,
    'twitter_card',
  ];
  $twitter = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'twitter:site',
      'content' => \Drupal::config('sharemessage.settings')
        ->get('twitter_user'),
    ],
  ];
  $tags[] = [
    $twitter,
    'twitter_site',
  ];
  $twitter = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'name' => 'twitter:description',
      'content' => $this
        ->getTokenizedField($this->message_long, $context),
    ],
  ];
  $tags[] = [
    $twitter,
    'twitter_description',
  ];
  if ($image_url = $this
    ->getImageUrl($context)) {
    $twitter = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'twitter:image',
        'content' => $image_url,
      ],
    ];
    $tags[] = [
      $twitter,
      'twitter_image',
    ];
  }
  return $tags;
}