diff --git a/core/modules/media/media.routing.yml b/core/modules/media/media.routing.yml index 9fbadeff29..c6734f8292 100644 --- a/core/modules/media/media.routing.yml +++ b/core/modules/media/media.routing.yml @@ -19,3 +19,15 @@ entity.media.revision: requirements: _access_media_revision: 'view' media: \d+ + +media.oembed.render: + path: '/media/oembed' + defaults: + _controller: '\Drupal\media\Controller\OEmbedController::render' + options: + parameters: + url: + type: string + + requirements: + _permission: 'access content' diff --git a/core/modules/media/src/Controller/OEmbedController.php b/core/modules/media/src/Controller/OEmbedController.php new file mode 100644 index 0000000000..d302a90ceb --- /dev/null +++ b/core/modules/media/src/Controller/OEmbedController.php @@ -0,0 +1,109 @@ +resourceFetcher = $resource_fetcher; + $this->oEmbedManager = $oembed_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('media.oembed.resource_fetcher'), + $container->get('media.oembed_manager') + ); + } + + public function render(Request $request) { + if (!($url = $request->get('url'))) { + throw new MissingMandatoryParametersException('url parameter not provided'); + } + + $element = []; + + + try { + $resource = $this->resourceFetcher->fetchResource($this->oEmbedManager->getResourceUrl($url /*$this->getSetting('max_width'), $this->getSetting('max_height') */)); + } + catch (ResourceException $exception) { + #$this->logger->error("Could not retrieve the remote URL (@url).", ['@url' => $item->{$main_property}]); + } + + switch ($resource['type']) { + case 'link': + $element = [ + '#title' => $resource['title'], + '#type' => 'link', + '#url' => Url::fromUri($url), + ]; + break; + + case 'photo': + $element = [ + '#theme' => 'image', + '#uri' => $resource['url'], + '#width' => $resource['width'], + '#height' => $resource['height'], + ]; + break; + + case 'video': + case 'rich': + + $element = [ + '#type' => 'inline_template', + '#template' => (string) $resource['html'], + ]; + break; + + default: + $this->logger->error("Unknown oEmbed resource type @type", ['@type' => $resource['type']]); + + } + + $response = \Drupal::service('renderer')->render($element); + + return new Response($response); + } + +} diff --git a/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php index 8f8f0a26df..f929085b62 100644 --- a/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php +++ b/core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php @@ -2,18 +2,10 @@ namespace Drupal\media\Plugin\Field\FieldFormatter; -use Drupal\Component\Utility\Xss; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Logger\LoggerChannelFactoryInterface; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; -use Drupal\media\OEmbed\OEmbedManagerInterface; -use Drupal\media\OEmbed\ResourceException; -use Drupal\media\OEmbed\ResourceFetcherInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the 'oembed' formatter. @@ -28,77 +20,7 @@ * }, * ) */ -class OEmbedFormatter extends FormatterBase implements ContainerFactoryPluginInterface { - - /** - * The oEmbed resource fetcher. - * - * @var \Drupal\media\OEmbed\ResourceFetcherInterface - */ - protected $resourceFetcher; - - /** - * The oEmbed manager. - * - * @var \Drupal\media\OEmbed\OEmbedManagerInterface - */ - protected $oEmbedManager; - - /** - * The logger service. - * - * @var \Drupal\Core\Logger\LoggerChannelInterface $logger - */ - protected $logger; - - /** - * Constructs a OEmbedFormatter instance. - * - * @param string $plugin_id - * The plugin ID for the formatter. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The definition of the field to which the formatter is associated. - * @param array $settings - * The formatter settings. - * @param string $label - * The formatter label display setting. - * @param string $view_mode - * The view mode. - * @param array $third_party_settings - * Any third party settings settings. - * @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher - * The resource fetcher service. - * @param \Drupal\media\OEmbed\OEmbedManagerInterface $oembed_manager - * The oEmbed manager service. - * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory - * The logger service. - */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ResourceFetcherInterface $resource_fetcher, OEmbedManagerInterface $oembed_manager, LoggerChannelFactoryInterface $logger_factory) { - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); - $this->resourceFetcher = $resource_fetcher; - $this->oEmbedManager = $oembed_manager; - $this->logger = $logger_factory->get('media'); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $plugin_id, - $plugin_definition, - $configuration['field_definition'], - $configuration['settings'], - $configuration['label'], - $configuration['view_mode'], - $configuration['third_party_settings'], - $container->get('media.oembed.resource_fetcher'), - $container->get('media.oembed_manager'), - $container->get('logger.factory') - ); - } +class OEmbedFormatter extends FormatterBase { /** * {@inheritdoc} @@ -124,46 +46,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) { continue; } - try { - $resource = $this->resourceFetcher->fetchResource($this->oEmbedManager->getResourceUrl($item->{$main_property}, $this->getSetting('max_width'), $this->getSetting('max_height'))); - } - catch (ResourceException $exception) { - $this->logger->error("Could not retrieve the remote URL (@url).", ['@url' => $item->{$main_property}]); - continue; - } - - switch ($resource['type']) { - case 'link': - $element[$delta] = [ - '#title' => $resource['title'], - '#type' => 'link', - '#url' => Url::fromUri($item->{$main_property}), - ]; - break; - - case 'photo': - $element[$delta] = [ - '#theme' => 'image', - '#uri' => $resource['url'], - '#width' => $resource['width'], - '#height' => $resource['height'], - ]; - break; - - case 'video': - case 'rich': - $tags = explode(',', $this->getSetting('allowed_html_tags')); - $tags = array_map('trim', $tags); - $element[$delta] = [ - '#type' => 'inline_template', - '#template' => Xss::filter((string) $resource['html'], array_merge($tags, Xss::getHtmlTagList())), - ]; - break; - - default: - $this->logger->error("Unknown oEmbed resource type @type", ['@type' => $resource['type']]); - - } + $element[$delta] = [ + '#type' => 'inline_template', + '#template' => '', + '#context' => [ + 'url' => Url::fromRoute('media.oembed.render', ['url' => $item->{$main_property}]), + ], + ]; } return $element;