You are here

public function Instagram::getMetadata in Media entity Instagram 8.2

Same name and namespace in other branches
  1. 3.x 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 MediaSourceBase::getMetadata

File

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

Class

Instagram
Provides media type plugin for Instagram.

Namespace

Drupal\media_entity_instagram\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $attribute_name) {
  if ($attribute_name == '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, 'username');
    $id = $this
      ->getMetadata($media, 'id');
    if ($username && $id) {
      return $username . ' - ' . $id;
    }
    else {
      $code = $this
        ->getMetadata($media, 'shortcode');
      if (!empty($code)) {
        return $code;
      }
    }

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

  // If we have auth settings return the other fields.
  if ($instagram = $this->fetcher
    ->fetchInstagramEmbed($matches['shortcode'])) {
    switch ($attribute_name) {
      case 'id':
        if (isset($instagram['media_id'])) {
          return $instagram['media_id'];
        }
        return FALSE;
      case 'type':
        if (isset($instagram['type'])) {
          return $instagram['type'];
        }
        return FALSE;
      case 'thumbnail':
        return 'http://instagram.com/p/' . $matches['shortcode'] . '/media/?size=m';
      case 'thumbnail_local':
        $local_uri = $this
          ->getMetadata($media, 'thumbnail_local_uri');
        if ($local_uri) {
          if (file_exists($local_uri)) {
            return $local_uri;
          }
          else {
            $directory = dirname($local_uri);
            $this->fileSystem
              ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
            $image_url = $this
              ->getMetadata($media, 'thumbnail');
            $response = $this->httpClient
              ->get($image_url);
            if ($response
              ->getStatusCode() == 200) {
              return $this->fileSystem
                ->saveData($response
                ->getBody(), $local_uri, FileSystemInterface::EXISTS_REPLACE);
            }
          }
        }
        return FALSE;
      case 'thumbnail_local_uri':
        if (isset($instagram['thumbnail_url'])) {
          return $this->configFactory
            ->get('media_entity_instagram.settings')
            ->get('local_images') . '/' . $matches['shortcode'] . '.' . pathinfo(parse_url($instagram['thumbnail_url'], PHP_URL_PATH), PATHINFO_EXTENSION);
        }
        return FALSE;
      case 'username':
        if (isset($instagram['author_name'])) {
          return $instagram['author_name'];
        }
        return FALSE;
      case 'caption':
        if (isset($instagram['title'])) {
          return $instagram['title'];
        }
        return FALSE;
    }
  }
  return FALSE;
}