You are here

public function Instagram::getMetadata in Media entity Instagram 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/media/Source/Instagram.php \Drupal\media_entity_instagram\Plugin\media\Source\Instagram::getMetadata()

Gets the value for a metadata attribute for a given media item.

Parameters

\Drupal\media\MediaInterface $media: A media item.

string $attribute_name: Name of the attribute to fetch.

Return value

mixed|null Metadata attribute value or NULL if unavailable.

Overrides OEmbed::getMetadata

File

src/Plugin/media/Source/Instagram.php, line 59

Class

Instagram
Implementation of an oEmbed Instagram source.

Namespace

Drupal\media_entity_instagram\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $name) {
  switch ($name) {
    case 'default_name':

      // Try to get some fields that need the API, if not available, just use
      // the shortcode as default name.
      $username = $this
        ->getMetadata($media, 'author_name');
      $shortcode = $this
        ->getMetadata($media, 'shortcode');
      if ($username && $shortcode) {
        return $username . ' - ' . $shortcode;
      }
      else {
        if (!empty($shortcode)) {
          return $shortcode;
        }
      }

      // Fallback to the parent's default name if everything else failed.
      return parent::getMetadata($media, 'default_name');
    case 'shortcode':
      $matches = $this
        ->matchRegexp($media);
      if (is_array($matches) && !empty($matches['shortcode'])) {
        return $matches['shortcode'];
      }
      return FALSE;
  }
  return parent::getMetadata($media, $name);
}