You are here

public function Soundcloud::getField in Media entity Soundcloud 8

@inheritDoc

Overrides MediaTypeInterface::getField

1 call to Soundcloud::getField()
Soundcloud::thumbnail in src/Plugin/MediaEntity/Type/Soundcloud.php
Gets thumbnail image.

File

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

Class

Soundcloud
Provides media type plugin for Soundcloud.

Namespace

Drupal\media_entity_soundcloud\Plugin\MediaEntity\Type

Code

public function getField(MediaInterface $media, $name) {
  if (($url = $this
    ->getMediaUrl($media)) && ($data = $this
    ->oEmbed($url))) {
    switch ($name) {
      case 'html':
        return $data['html'];
      case 'thumbnail_uri':
        if (isset($data['thumbnail_url'])) {
          $destination = $this->configFactory
            ->get('media_entity_soundcloud.settings')
            ->get('thumbnail_destination');
          $local_uri = $destination . '/' . pathinfo($data['thumbnail_url'], PATHINFO_BASENAME);

          // Save the file if it does not exist.
          if (!file_exists($local_uri)) {
            file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
            $image = file_get_contents($data['thumbnail_url']);
            file_unmanaged_save_data($image, $local_uri, FILE_EXISTS_REPLACE);
            return $local_uri;
          }
        }
        return FALSE;
      case 'track_id':

        // Extract the src attribute from the html code.
        preg_match('/src="([^"]+)"/', $data['html'], $src_matches);
        if (!count($src_matches)) {
          return FALSE;
        }

        // Extract the id from the src.
        preg_match('/\\/tracks\\/(\\d*)/', urldecode($src_matches[1]), $matches);
        if (!count($matches)) {
          return FALSE;
        }
        return $matches[1];
    }
  }
  return FALSE;
}