View source
<?php
namespace Drupal\media_entity_soundcloud\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media_entity_soundcloud\Plugin\media\Source\Soundcloud;
use Drupal\media_library\Form\AddFormBase;
use Drupal\media_library\MediaLibraryUiBuilder;
use Drupal\media_library\OpenerResolverInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SoundcloudForm extends AddFormBase {
protected $httpClient;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MediaLibraryUiBuilder $library_ui_builder, ClientInterface $http_client, OpenerResolverInterface $opener_resolver = NULL) {
parent::__construct($entity_type_manager, $library_ui_builder, $opener_resolver);
$this->httpClient = $http_client;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('media_library.ui_builder'), $container
->get('http_client'), $container
->get('media_library.opener_resolver'));
}
protected function getMediaType(FormStateInterface $form_state) {
if ($this->mediaType) {
return $this->mediaType;
}
$media_type = parent::getMediaType($form_state);
if (!$media_type
->getSource() instanceof Soundcloud) {
throw new \InvalidArgumentException('Can only add media types which use an Soundcloud source plugin.');
}
return $media_type;
}
protected function buildInputElement(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$container = [
'#type' => 'container',
];
$container['soundcloud_url'] = [
'#type' => 'textfield',
'#title' => $this
->t("Add Soundcloud Track"),
];
$container['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#button_type' => 'primary',
'#submit' => [
'::addButtonSubmit',
],
'#validate' => [
'::validateSoundcloudUrl',
],
'#ajax' => [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
'url' => Url::fromRoute('media_library.ui'),
'options' => [
'query' => $this
->getMediaLibraryState($form_state)
->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
],
];
$form['container'] = $container;
return $form;
}
public function validateSoundcloudUrl(array &$form, FormStateInterface $form_state) {
$url = $form_state
->getValue('soundcloud_url');
if (preg_match('/https?:\\/\\/(www\\.)?soundcloud\\.com\\/.*/', $url)) {
try {
\Drupal::httpClient()
->get($url);
} catch (ClientException $e) {
$form_state
->setErrorByName('url', $this
->t('Could not connect to the track, please make sure that the url is correct.'));
}
}
else {
$form_state
->setErrorByName('url', $this
->t('Invalid url.'));
}
}
public function addButtonSubmit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$this
->processInputValues([
$form_state
->getValue('soundcloud_url'),
], $form, $form_state);
}
function getFormId() {
return 'soundcloud_media_add_form';
}
}