You are here

public function BlocksRendererHelper::getBlockFromPluginId in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/BlocksRendererHelper.php \Drupal\gutenberg\BlocksRendererHelper::getBlockFromPluginId()

Create instance of block plugin from given ID and config.

Parameters

string $id: Block Plugin ID.

array $config: Block configuration.

Return value

\Drupal\Core\Block\BlockPluginInterface|null Block Plugin instance or null.

Throws

\Drupal\Component\Plugin\Exception\ContextException

File

src/BlocksRendererHelper.php, line 122

Class

BlocksRendererHelper
Class BlocksRendererHelper.

Namespace

Drupal\gutenberg

Code

public function getBlockFromPluginId($id, array $config = []) {
  try {
    $block_instance = $this->blockManager
      ->createInstance($id, $config);

    // Apply runtime contexts.
    if ($block_instance instanceof ContextAwarePluginInterface) {
      $contexts = $this->contextRepository
        ->getRuntimeContexts($block_instance
        ->getContextMapping());
      $this->contextHandler
        ->applyContextMapping($block_instance, $contexts);
    }
    return $block_instance;
  } catch (PluginException $e) {
    $this->logger
      ->error($e
      ->getMessage());
    return NULL;
  }
}