You are here

public function Instagram::getField in Media entity Instagram 8

Gets a media-related field/value.

Parameters

MediaInterface $media: Media object.

string $name: Name of field to fetch.

Return value

mixed Field value or FALSE if data unavailable.

Overrides MediaTypeInterface::getField

2 calls to Instagram::getField()
Instagram::getDefaultName in src/Plugin/MediaEntity/Type/Instagram.php
Provide a default name for the media.
Instagram::thumbnail in src/Plugin/MediaEntity/Type/Instagram.php
Gets thumbnail image.

File

src/Plugin/MediaEntity/Type/Instagram.php, line 119

Class

Instagram
Provides media type plugin for Instagram.

Namespace

Drupal\media_entity_instagram\Plugin\MediaEntity\Type

Code

public function getField(MediaInterface $media, $name) {
  $matches = $this
    ->matchRegexp($media);
  if (!$matches['shortcode']) {
    return FALSE;
  }
  if ($name == 'shortcode') {
    return $matches['shortcode'];
  }

  // If we have auth settings return the other fields.
  if ($instagram = $this->fetcher
    ->fetchInstagramEmbed($matches['shortcode'])) {
    switch ($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
          ->getField($media, 'thumbnail_local_uri');
        if ($local_uri) {
          if (file_exists($local_uri)) {
            return $local_uri;
          }
          else {
            $directory = dirname($local_uri);
            file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
            $image_url = $this
              ->getField($media, 'thumbnail');
            $response = $this->httpClient
              ->get($image_url);
            if ($response
              ->getStatusCode() == 200) {
              return file_unmanaged_save_data($response
                ->getBody(), $local_uri, FILE_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;
}