You are here

class ReusableBlockProcessor in Gutenberg 8.2

Processes Gutenberg reusable blocks.

Hierarchy

Expanded class hierarchy of ReusableBlockProcessor

1 string reference to 'ReusableBlockProcessor'
gutenberg.services.yml in ./gutenberg.services.yml
gutenberg.services.yml
1 service uses ReusableBlockProcessor
gutenberg.block_processor_resusable_blocks in ./gutenberg.services.yml
Drupal\gutenberg\BlockProcessor\ReusableBlockProcessor

File

src/BlockProcessor/ReusableBlockProcessor.php, line 15

Namespace

Drupal\gutenberg\BlockProcessor
View source
class ReusableBlockProcessor implements GutenbergBlockProcessorInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\Render\RendererInterface instance.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The Gutenberg logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * ReusableBlockProcessor constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Psr\Log\LoggerInterface $logger
   *   Gutenberg logger interface.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, LoggerInterface $logger) {
    $this->entityTypeManager = $entity_type_manager;
    $this->renderer = $renderer;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function processBlock(array &$block, &$block_content, RefinableCacheableDependencyInterface $bubbleable_metadata) {
    $block_attributes = $block['attrs'];
    if (empty($block_attributes['ref'])) {

      // This should not happen, and typically means there's a bug somewhere.
      $this->logger
        ->error('Missing reusable block reference ID.');
      return;
    }
    $bid = $block_attributes['ref'];
    $block_entity = BlockContent::load($bid);
    if ($block_entity) {
      $render = $this->entityTypeManager
        ->getViewBuilder('block_content')
        ->view($block_entity, 'reusable_block');
      $block_content = $this->renderer
        ->render($render);
      $bubbleable_metadata
        ->addCacheableDependency(CacheableMetadata::createFromRenderArray($render));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isSupported(array $block, $block_content = '') {
    return $block['blockName'] === 'core/block';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReusableBlockProcessor::$entityTypeManager protected property The entity type manager.
ReusableBlockProcessor::$logger protected property The Gutenberg logger.
ReusableBlockProcessor::$renderer protected property Drupal\Core\Render\RendererInterface instance.
ReusableBlockProcessor::isSupported public function Whether the processor supports this block instance. Overrides GutenbergBlockProcessorInterface::isSupported
ReusableBlockProcessor::processBlock public function Process the Gutenberg block and its content. Overrides GutenbergBlockProcessorInterface::processBlock
ReusableBlockProcessor::__construct public function ReusableBlockProcessor constructor.