You are here

private function BlockFilter::cacheabilityMetadata in Gutenberg 8

Helper function to collect cache metadata from Drupal blocks.

Parameters

string $text:

Return value

array

1 call to BlockFilter::cacheabilityMetadata()
BlockFilter::process in src/Plugin/Filter/BlockFilter.php
Process each URL.

File

src/Plugin/Filter/BlockFilter.php, line 132

Class

BlockFilter
Class BlockFilter.

Namespace

Drupal\gutenberg\Plugin\Filter

Code

private function cacheabilityMetadata($text) {
  $metadata = [
    'tags' => [],
    'contexts' => [],
    'max-age' => 0,
  ];
  preg_match_all('#<!-- wp:drupalblock\\/.*\\s(\\{.*\\})\\s\\/-->#', $text, $matches);
  if (!empty($matches[1])) {
    foreach ($matches[1] as $match) {
      $attributes = json_decode($match);
      $block = $this->blocksRenderer
        ->getBlockFromPluginId($attributes->blockId, []);
      $build = $block
        ->build();
      $metadata['tags'] = array_merge($metadata['tags'], isset($build['#cache']['tags']) ? $build['#cache']['tags'] : $block
        ->getCacheTags());
      $metadata['contexts'] = array_merge($metadata['contexts'], isset($build['#cache']['contexts']) ? $build['#cache']['contexts'] : $block
        ->getCacheContexts());
      $max_age = isset($build['#cache']['max-age']) ? $build['#cache']['max-age'] : $block
        ->getCacheMaxAge();
      if ($max_age < $metadata['max-age']) {
        $metadata['max-age'] = $max_age;
      }
    }
    $metadata['tags'] = array_unique($metadata['tags']);
    $metadata['contexts'] = array_unique($metadata['contexts']);
  }
  return $metadata;
}