You are here

class DrupalBlockProcessor in Gutenberg 8.2

Processes Drupal blocks than can be embedded.

Hierarchy

Expanded class hierarchy of DrupalBlockProcessor

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

File

src/BlockProcessor/DrupalBlockProcessor.php, line 16

Namespace

Drupal\gutenberg\BlockProcessor
View source
class DrupalBlockProcessor implements GutenbergBlockProcessorInterface {
  use StringTranslationTrait;

  /**
   * Drupal\gutenberg\BlocksRendererHelper instance.
   *
   * @var \Drupal\gutenberg\BlocksRendererHelper
   */
  protected $blocksRenderer;

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

  /**
   * DynamicRenderProcessor constructor.
   *
   * @param \Drupal\gutenberg\BlocksRendererHelper $blocks_renderer
   *   The block renderer.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(BlocksRendererHelper $blocks_renderer, RendererInterface $renderer) {
    $this->blocksRenderer = $blocks_renderer;
    $this->renderer = $renderer;
  }

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

    /* TODO: Check if the block is currently in the "allowed" list?.
     *  Don't think this is possible in the backend as the content has no
     *  direct reference to the parent entity. Might have to be handled in the
     *  Gutenberg editor on load.
     */
    $plugin_id = $block_attributes['blockId'];

    // TODO: load the config from the block attributes.
    $config = $block_attributes['config'] ?? [];
    $plugin_block = $this->blocksRenderer
      ->getBlockFromPluginId($plugin_id, $config);
    if ($plugin_block) {
      $access_result = $this->blocksRenderer
        ->getBlockAccess($plugin_block);
      $bubbleable_metadata
        ->addCacheableDependency($access_result);
      if ($access_result
        ->isForbidden()) {

        /*
         * Add as a comment in the HTML.
         * Fixme: Is this a good idea (as we're exposing backend info to the
         *  frontend)? I'm leaning towards removing it.
         */
        $render_content = [
          '#prefix' => Markup::create('<!-- '),
          '#markup' => $this
            ->t('Block access denied: @plugin_id', [
            '@plugin_id' => $plugin_id,
          ]),
          '#suffix' => Markup::create(' -->'),
        ];
      }
      else {
        $render_content = $this->blocksRenderer
          ->getRenderFromBlockPlugin($plugin_block, FALSE);
      }
      $render = [
        'content' => $render_content,
      ];

      // Render the css class if available.
      if (!empty($block_attributes['className'])) {
        $render['#prefix'] = sprintf('<div class="%s">', Html::escape($block_attributes['className']));
        $render['#suffix'] = '</div>';
      }
      $block_content = $this->renderer
        ->render($render);
      $bubbleable_metadata
        ->addCacheableDependency(CacheableMetadata::createFromRenderArray($render));
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalBlockProcessor::$blocksRenderer protected property Drupal\gutenberg\BlocksRendererHelper instance.
DrupalBlockProcessor::$renderer protected property Drupal\Core\Render\RendererInterface instance.
DrupalBlockProcessor::isSupported public function Whether the processor supports this block instance. Overrides GutenbergBlockProcessorInterface::isSupported
DrupalBlockProcessor::processBlock public function Process the Gutenberg block and its content. Overrides GutenbergBlockProcessorInterface::processBlock
DrupalBlockProcessor::__construct public function DynamicRenderProcessor constructor.
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.