You are here

public function OEmbedProcessor::processBlock in Gutenberg 8.2

Process the Gutenberg block and its content.

The content and block can be manipulated here. Return FALSE to ensure that no other plugins are ran after this instance.

Parameters

array $block: The block object.

string $block_content: The inner block content.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $bubbleable_metadata: The bubbleable metadata.

Return value

bool|null Return FALSE if further processing should be stopped.

Overrides GutenbergBlockProcessorInterface::processBlock

File

src/BlockProcessor/OEmbedProcessor.php, line 91

Class

OEmbedProcessor
Processes oEmbed blocks.

Namespace

Drupal\gutenberg\BlockProcessor

Code

public function processBlock(array &$block, &$block_content, RefinableCacheableDependencyInterface $bubbleable_metadata) {
  $block_attributes = $block['attrs'];
  $url = $block_attributes['url'];

  // Try and check against the cache as too many requests might lead to
  // the site being blacklisted.
  $maxwidth = $this->settings['oembed']['maxwidth'];
  $cache_id = "gutenberg:oembed_processor:{$url}:{$maxwidth}";
  $cached = $this
    ->cacheGet($cache_id);
  if ($cached) {
    if ($cached->data) {

      // Only replace if there's cache data.
      $block_content = str_replace($url, $cached->data, $block_content);
    }
    return;
  }
  $regex = NULL;
  $provider_uri = NULL;
  list($regex, $provider_uri) = $this
    ->getProviderUri($url);
  $output = NULL;
  if ($regex === 'LOCAL' && !empty($provider_uri)) {

    // Local provider configuration.
    $output = $this
      ->getLocalProviderContent($provider_uri, $url);
  }
  else {
    $query_params = rawurldecode(UrlHelper::buildQuery([
      'url' => $url,
      'origin' => 'drupal',
      'format' => 'json',
      'maxwidth' => $maxwidth,
    ]));
    if (!empty($provider_uri)) {

      // User-defined provider.
      $arg_separator = strpos($provider_uri, '?') === FALSE ? '?' : '&';
      $output = $this->oembedResolver
        ->fetchOembedHtml($provider_uri . $arg_separator . $query_params);
    }
    else {
      $output = $this->oembedResolver
        ->resolveOembed($url, $maxwidth);
    }
  }
  if ($output === NULL) {

    // Cache the empty result for 5 minutes, if no response was received,
    // possibly due to network issues.
    $max_age = 5 * 60;
    $expiry = $this->time
      ->getRequestTime() + $max_age;
  }
  else {
    $expiry = Cache::PERMANENT;
  }
  $this
    ->cacheSet($cache_id, $output, $expiry);
  if ($output) {

    // Replace the oEmbed link.
    $block_content = str_replace($url, $output, $block_content);
  }
}