class Soundcloud 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
Soundcloud entity media source.
Plugin annotation
@MediaSource(
id = "soundcloud",
label = @Translation("Soundcloud"),
allowed_field_types = {"string", "string_long", "link"},
default_thumbnail_filename = "soundcloud.png",
description = @Translation("Provides business logic and metadata for Soundcloud."),
forms = {
"media_library_add" = "\Drupal\media_entity_soundcloud\Form\SoundcloudForm"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\media\MediaSourceBase implements ContainerFactoryPluginInterface, MediaSourceInterface
- class \Drupal\media_entity_soundcloud\Plugin\media\Source\Soundcloud
- class \Drupal\media\MediaSourceBase implements ContainerFactoryPluginInterface, MediaSourceInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Soundcloud
2 files declare their use of Soundcloud
- SoundcloudEmbedFormatter.php in src/
Plugin/ Field/ FieldFormatter/ SoundcloudEmbedFormatter.php - SoundcloudForm.php in src/
Form/ SoundcloudForm.php
1 string reference to 'Soundcloud'
- SoundcloudEmbedFormatterTest::testSoundcloudEmbedFormatter in tests/
src/ Functional/ SoundcloudEmbedFormatterTest.php - Tests adding and editing a soundcloud embed formatter.
File
- src/
Plugin/ media/ Source/ Soundcloud.php, line 30
Namespace
Drupal\media_entity_soundcloud\Plugin\media\SourceView source
class Soundcloud extends MediaSourceBase {
/**
* Soundcloud attributes.
*
* @var array
*/
protected $soundcloud;
/**
* Config factory interface.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Http Client Interface.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ClientInterface $httpClient) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->httpClient = $httpClient;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('config.factory'), $container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
$attributes = [
'track_id' => $this
->t('The track id - not always available'),
'playlist_id' => $this
->t('The playlist (set) id - not always available'),
'source_id' => t('Compound of source type (track or playlist) and id so that it is unique among all SoundCloud media'),
'html' => $this
->t('HTML embed code'),
'thumbnail_uri' => t('URI of the thumbnail'),
];
return $attributes;
}
/**
* {@inheritdoc}
*/
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);
}
}
/**
* Returns the track id from the source_url_field.
*
* @param \Drupal\media\MediaInterface $media
* The media entity.
*
* @return string|bool
* The track if from the source_url_field if found. False otherwise.
*/
protected function getMediaUrl(MediaInterface $media) {
$source_field = $this
->getSourceFieldDefinition($media->bundle->entity);
$field_name = $source_field
->getName();
if ($media
->hasField($field_name)) {
$property_name = $source_field
->getFieldStorageDefinition()
->getMainPropertyName();
return $media->{$field_name}->{$property_name};
}
return FALSE;
}
/**
* Returns oembed data for a Soundcloud url.
*
* @param string $url
* The Soundcloud Url.
*
* @return array
* An array of oembed data.
*/
protected function oEmbed($url) {
$this->soundcloud =& drupal_static(__FUNCTION__ . hash('md5', $url));
if (!isset($this->soundcloud)) {
$url = 'https://soundcloud.com/oembed?format=json&url=' . $url;
try {
$response = $this->httpClient
->get($url);
$this->soundcloud = Json::decode((string) $response
->getBody());
} catch (ClientException $e) {
$this->soundcloud = FALSE;
}
}
return $this->soundcloud;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MediaSourceBase:: |
protected | property | The entity field manager service. | |
MediaSourceBase:: |
protected | property | The entity type manager service. | |
MediaSourceBase:: |
protected | property | The field type plugin manager service. | |
MediaSourceBase:: |
protected | property | Plugin label. | |
MediaSourceBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
2 |
MediaSourceBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
MediaSourceBase:: |
public | function |
Creates the source field definition for a type. Overrides MediaSourceInterface:: |
2 |
MediaSourceBase:: |
protected | function | Creates the source field storage definition. | |
MediaSourceBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
2 |
MediaSourceBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
MediaSourceBase:: |
public | function |
Get the source field definition for a media type. Overrides MediaSourceInterface:: |
|
MediaSourceBase:: |
protected | function | Determine the name of the source field. | 2 |
MediaSourceBase:: |
protected | function | Get the source field options for the media type form. | |
MediaSourceBase:: |
protected | function | Returns the source field storage definition. | |
MediaSourceBase:: |
public | function |
Get the primary value stored in the source field. Overrides MediaSourceInterface:: |
|
MediaSourceBase:: |
public | function |
Prepares the media type fields for this source in the form display. Overrides MediaSourceInterface:: |
3 |
MediaSourceBase:: |
public | function |
Prepares the media type fields for this source in the view display. Overrides MediaSourceInterface:: |
6 |
MediaSourceBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
MediaSourceBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
1 |
MediaSourceBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
MediaSourceInterface:: |
constant | Default empty value for metadata fields. | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
Soundcloud:: |
protected | property |
Config factory interface. Overrides MediaSourceBase:: |
|
Soundcloud:: |
protected | property | Http Client Interface. | |
Soundcloud:: |
protected | property | Soundcloud attributes. | |
Soundcloud:: |
public static | function |
Creates an instance of the plugin. Overrides MediaSourceBase:: |
|
Soundcloud:: |
protected | function | Returns the track id from the source_url_field. | |
Soundcloud:: |
public | function |
Gets the value for a metadata attribute for a given media item. Overrides MediaSourceBase:: |
|
Soundcloud:: |
public | function |
Gets a list of metadata attributes provided by this plugin. Overrides MediaSourceInterface:: |
|
Soundcloud:: |
protected | function | Returns oembed data for a Soundcloud url. | |
Soundcloud:: |
public | function |
Constructs a new class instance. Overrides MediaSourceBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |