You are here

class Embed in CKEditor Media Embed Plugin 8

The default CKEditor Media Embed class.

Hierarchy

Expanded class hierarchy of Embed

1 string reference to 'Embed'
ckeditor_media_embed.services.yml in ./ckeditor_media_embed.services.yml
ckeditor_media_embed.services.yml
1 service uses Embed
ckeditor_media_embed in ./ckeditor_media_embed.services.yml
Drupal\ckeditor_media_embed\Embed

File

src/Embed.php, line 23

Namespace

Drupal\ckeditor_media_embed
View source
class Embed implements EmbedInterface {
  use StringTranslationTrait;

  /**
   * The HTTP client to fetch the embed code with.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * The unrouted URL assembler service.
   *
   * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface
   */
  protected $urlAssembler;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The current path service.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs an Embed object.
   *
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The http client used to do retrieval of embed codes.
   * @param \Drupal\Core\Utility\UnroutedUrlAssemblerInterface $url_assembler
   *   The url assembler used to create url from a parsed url.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ClientInterface $http_client, UnroutedUrlAssemblerInterface $url_assembler, RequestStack $request_stack, MessengerInterface $messenger, ConfigFactory $config_factory, CurrentPathStack $current_path, ModuleHandlerInterface $module_handler) {
    $this->httpClient = $http_client;
    $this->urlAssembler = $url_assembler;
    $this->requestStack = $request_stack;
    $this->configFactory = $config_factory;
    $this->messenger = $messenger;
    $this->currentPath = $current_path;
    $this->moduleHandler = $module_handler;
    $embed_provider = $this->configFactory
      ->get('ckeditor_media_embed.settings')
      ->get('embed_provider');
    $this
      ->setEmbedProvider($embed_provider);
  }

  /**
   * {@inheritdoc}
   */
  public function setEmbedProvider($provider) {
    $provider_parsed = UrlHelper::parse($provider);
    $provider_parsed['query'] = array_filter($provider_parsed['query'], function ($value) {
      return $value !== '{callback}';
    });
    $provider_parsed['absolute'] = TRUE;
    $this->embed_provider = $this->urlAssembler
      ->assemble($provider_parsed['path'], $provider_parsed);
  }

  /**
   * {@inheritdoc}
   */
  public function getEmbedObject($url) {
    $embed = NULL;
    try {
      $response = $this->httpClient
        ->get($this
        ->getEmbedProviderURL($url), [
        'headers' => [
          'content-type' => 'application/json',
        ],
      ]);
      $embed = json_decode($response
        ->getBody());
    } catch (TransferException $e) {
      $this->messenger
        ->addWarning($this
        ->t('Unable to retrieve @url at this time, please check again later.', [
        '@url' => $url,
      ]));
      watchdog_exception('ckeditor_media_embed', $e);
    }
    $this->moduleHandler
      ->alter('ckeditor_media_embed_object', $embed);
    return $embed;
  }

  /**
   * Inject the media url into the provider url.
   *
   * @return string
   *   The provider url with the media url injected.
   */

  // @codingStandardsIgnoreLine
  protected function getEmbedProviderURL($url) {
    $provider = $this->embed_provider;
    if (strpos($provider, '//') === 0) {
      $provider = $this->requestStack
        ->getCurrentRequest()
        ->getScheme() . ':' . $provider;
    }
    return str_replace('%7Burl%7D', urlencode($url), $provider);
  }

  /**
   * {@inheritdoc}
   */
  public function processEmbeds($text) {
    $document = Html::load($text);
    $xpath = new \DOMXPath($document);
    foreach ($xpath
      ->query('//oembed') as $node) {
      $embed = $this
        ->getEmbedObject($node->nodeValue);
      if (!empty($embed) && !empty($embed->html)) {
        $this
          ->swapEmbedHtml($node, $embed);
      }
    }
    return Html::serialize($document);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsLink() {
    $url = Url::fromRoute('ckeditor_media_embed.ckeditor_media_embed_settings_form', [
      'destination' => $this->currentPath
        ->getPath(),
    ]);
    return Markup::create(Link::fromTextAndUrl($this
      ->t('CKEditor Media Embed plugin settings page'), $url)
      ->toString());
  }

  /**
   * Replace <oembed> tags with their respected embed HTML.
   *
   * @param \DOMNode $node
   *   The DOMNode object of the <oembed> tag.
   * @param object $embed
   *   The embed json decoded object as provided by Embed::getEmbedOjbect().
   *
   * @return $this
   */
  protected function swapEmbedHtml(\DOMNode &$node, $embed) {
    $embed_node = $node->ownerDocument
      ->createElement('div');
    $embed_node
      ->setAttribute('class', $this
      ->getClass($embed));
    $child = NULL;
    $embed_body_node = Html::load(trim($embed->html))
      ->getElementsByTagName('body')
      ->item(0);
    foreach ($embed_body_node->childNodes as $child) {
      if ($child = $node->ownerDocument
        ->importNode($child, TRUE)) {
        $embed_node
          ->appendChild($child);
      }
    }
    $node->parentNode
      ->replaceChild($embed_node, $node);
    return $this;
  }

  /**
   * Retrieve the HTML class to apply to the new embed html node.
   *
   * @param object $embed
   *   The embed json decoded object as provided by Embed::getEmbedOjbect().
   *
   * @return string
   *   The safe HTML class string to apply the new embed html node.
   */
  protected function getClass($embed) {
    return 'embed-media ' . Html::getClass("embed-media--{$embed->type}-{$embed->provider_name}");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Embed::$configFactory protected property The config factory service.
Embed::$currentPath protected property The current path service.
Embed::$httpClient protected property The HTTP client to fetch the embed code with.
Embed::$messenger protected property The messenger.
Embed::$moduleHandler protected property The module handler.
Embed::$requestStack protected property The request stack.
Embed::$urlAssembler protected property The unrouted URL assembler service.
Embed::getClass protected function Retrieve the HTML class to apply to the new embed html node.
Embed::getEmbedObject public function Retrieve the Embed object as provided by the embed provider. Overrides EmbedInterface::getEmbedObject
Embed::getEmbedProviderURL protected function
Embed::getSettingsLink public function Retrieve the link to the configuration page for the settings. Overrides EmbedInterface::getSettingsLink
Embed::processEmbeds public function Replace all oembed tags with the embed html based ona provider resource. Overrides EmbedInterface::processEmbeds
Embed::setEmbedProvider public function Sets specified provider url as the provider URL. Overrides EmbedInterface::setEmbedProvider
Embed::swapEmbedHtml protected function Replace <oembed> tags with their respected embed HTML.
Embed::__construct public function Constructs an Embed object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.