public function Soundcloud::getMetadata in Media entity Soundcloud 3.x
Same name and namespace in other branches
- 8.2 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 95
Class
- Soundcloud
- Soundcloud entity media source.
Namespace
Drupal\media_entity_soundcloud\Plugin\media\SourceCode
public function getMetadata(MediaInterface $media, $attribute_name) {
$file_system = \Drupal::service('file_system');
$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_system
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$image = file_get_contents($data['thumbnail_url']);
$file_system
->saveData($image, $local_uri, FileSystemInterface::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);
}
}