You are here

public function ShareMessage::buildOGTags in Share Message 8

Returns Open Graph meta tags for <head>.

Parameters

array $context: The context for the token replacements.

Return value

array An array containing the Open Graph meta tags:

  • title
  • image: If at least one image Url is given.
  • video, video:width, video:height, video:type: If at least one video Url is given.
  • url
  • description
  • type

Overrides ShareMessageInterface::buildOGTags

File

src/Entity/ShareMessage.php, line 274

Class

ShareMessage
Entity class for the Share Message entity.

Namespace

Drupal\sharemessage\Entity

Code

public function buildOGTags($context) {
  $tags = [];

  // Base value for og:type meta tag.
  // @todo don't hardcode this, make configurable per Share Message entity.
  $type = 'website';

  // OG: Title.
  $tags[] = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'property' => 'og:title',
      'content' => $this
        ->getTokenizedField($this->title, $context),
    ],
  ];

  // OG: Image, also used for video thumbnail.
  if ($image_url = $this
    ->getImageUrl($context, $fallback_image)) {
    $tags[] = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'og:image',
        'content' => $image_url,
      ],
    ];
    $image_width = NULL;
    $image_height = NULL;
    if ($fallback_image instanceof FileInterface) {
      if (file_exists($fallback_image
        ->getFileUri())) {
        $image = \Drupal::service('image.factory')
          ->get($fallback_image
          ->getFileUri());
        if ($image
          ->isValid()) {
          $image_width = $image
            ->getWidth();
          $image_height = $image
            ->getHeight();
        }
      }
    }
    else {
      $image_width = $this
        ->getTokenizedField($this->image_width, $context);
      $image_height = $this
        ->getTokenizedField($this->image_height, $context);
    }
    if ($image_width && $image_height) {
      $tags[] = [
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => [
          'property' => 'og:image:width',
          'content' => $image_width,
        ],
      ];
      $tags[] = [
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => [
          'property' => 'og:image:height',
          'content' => $image_height,
        ],
      ];
    }
  }

  // OG: Video.
  if ($video_url = $this
    ->getTokenizedField($this->video_url, $context)) {
    $tags[] = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'og:video',
        'content' => $video_url . '?fs=1',
      ],
    ];
    $tags[] = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'og:video:width',
        'content' => \Drupal::config('sharemessage.addthis')
          ->get('shared_video_width'),
      ],
    ];
    $tags[] = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'og:video:height',
        'content' => \Drupal::config('sharemessage.addthis')
          ->get('shared_video_height'),
      ],
    ];
    $tags[] = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'property' => 'og:video:type',
        'content' => 'application/x-shockwave-flash',
      ],
    ];

    // Override og:type to video.
    $type = 'video';
  }

  // OG: URL.
  $tags[] = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'property' => 'og:url',
      'content' => $this
        ->getUrl($context),
    ],
  ];

  // OG: Description.
  $tags[] = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'property' => 'og:description',
      'content' => $this
        ->getTokenizedField($this->message_long, $context),
    ],
  ];

  // OG: Type.
  $tags[] = [
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => [
      'property' => 'og:type',
      'content' => $type,
    ],
  ];
  return $tags;
}