public function Soundcloud::getMetadata in Media entity Soundcloud 8.2
Same name and namespace in other branches
- 3.x src/Plugin/media/Source/Soundcloud.php \Drupal\media_entity_soundcloud\Plugin\media\Source\Soundcloud::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/ Soundcloud.php, line 94
Class
- Soundcloud
- Soundcloud entity media source.
Namespace
Drupal\media_entity_soundcloud\Plugin\media\SourceCode
public function getMetadata(MediaInterface $media, $attribute_name) {
$content_url = $this
->getMediaUrl($media);
if ($content_url === FALSE) {
return FALSE;
}
$data = $this
->oEmbed($content_url);
if ($data === FALSE) {
return FALSE;
}
switch ($attribute_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 parent::getMetadata($media, $attribute_name);
case 'track_id':
case 'playlist_id':
case 'source_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|playlists)/(\\d+)#', urldecode($src_matches[1]), $matches);
if (!count($matches)) {
return FALSE;
}
if ($attribute_name == 'source_id') {
return $matches[1] . '/' . $matches[2];
}
elseif ($attribute_name == 'track_id' && $matches[1] == 'tracks' || $attribute_name == 'playlist_id' && $matches[1] == 'playlists') {
return $matches[2];
}
return FALSE;
default:
return parent::getMetadata($media, $attribute_name);
}
}