View source
<?php
namespace Drupal\media_entity_soundcloud\Plugin\MediaEntity\Type;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Soundcloud extends MediaTypeBase {
protected $soundcloud;
protected $configFactory;
protected $httpClient;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $configFactory, ClientInterface $httpClient) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $configFactory
->get('media_entity.settings'));
$this->configFactory = $configFactory;
$this->httpClient = $httpClient;
}
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('config.factory'), $container
->get('http_client'));
}
public function defaultConfiguration() {
return [
'source_url_field' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = [];
$options = [
'' => $this
->t('Select'),
];
$bundle = $form_state
->getFormObject()
->getEntity();
$allowed_field_types = [
'string',
'string_long',
'link',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types) && !$field
->getFieldStorageDefinition()
->isBaseField()) {
$options[$field_name] = $field
->getLabel();
}
}
$disabled = !count($options);
if ($disabled) {
$options = [
'' => $this
->t('Add fields to the media bundle'),
];
}
$form['source_url_field'] = [
'#type' => 'select',
'#title' => t('Soundcloud URL source field'),
'#description' => t('Select the field on the media entity that stores Soundcloud URL.'),
'#default_value' => isset($this->configuration['source_url_field']) ? $this->configuration['source_url_field'] : '',
'#options' => $options,
'#disabled' => $disabled,
];
return $form;
}
public function providedFields() {
return [
'track_id' => $this
->t('The track id'),
'html' => $this
->t('HTML embed code'),
'thumbnail_uri' => t('URI of the thumbnail'),
];
}
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);
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':
preg_match('/src="([^"]+)"/', $data['html'], $src_matches);
if (!count($src_matches)) {
return FALSE;
}
preg_match('/\\/tracks\\/(\\d*)/', urldecode($src_matches[1]), $matches);
if (!count($matches)) {
return FALSE;
}
return $matches[1];
}
}
return FALSE;
}
public function thumbnail(MediaInterface $media) {
if ($thumbnail_image = $this
->getField($media, 'thumbnail_uri')) {
return $thumbnail_image;
}
return $this
->getDefaultThumbnail();
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/soundcloud.png';
}
protected function getMediaUrl(MediaInterface $media) {
if (isset($this->configuration['source_url_field'])) {
$source_url_field = $this->configuration['source_url_field'];
if ($media
->hasField($source_url_field)) {
if (!empty($media->{$source_url_field}
->first())) {
$property_name = $media->{$source_url_field}
->first()
->mainPropertyName();
return $media->{$source_url_field}->{$property_name};
}
}
}
return FALSE;
}
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;
$response = $this->httpClient
->get($url);
$this->soundcloud = json_decode((string) $response
->getBody(), TRUE);
}
return $this->soundcloud;
}
}