0

I have created derivative blocks using a method such as this and want to get the contents of a specific derivative block.

I can see all the available blocks (even derivative blocks) that have been assigned to a region with

 $ids = \Drupal::entityQuery('block')->execute();

but can't see unassigned blocks

I have a feeling that I need to use the code from berdir's answer b)

$variables['block_output'] = \Drupal::service('plugin.manager.block')
   ->createInstance($plugin, $configuration)
   ->build();

but don't know what to use for $plugin or $configuration

any help example will be appreciated

1 Answer 1

2

It sems this will do the trick:

  $blockManager = \Drupal::service('plugin.manager.block');
  //see all available block plugins (even not instantiated)
  $contextRepository = \Drupal::service('context.repository');
  $definitions = $blockManager->getDefinitionsForContexts($contextRepository->getAvailableContexts());

Here is how I used this info:

function TEMPLATE_preprocess_node(&$vars) {
  $blockManager = \Drupal::service('plugin.manager.block');   
  $vars['content']['derivative_block'] = $blockManager->createInstance('module_name:block_derivative_id')->build();
}

If you enable the commented lines $definitions will get you all available blocks

Credit goes out to enzo for this solution.

More info about programmatic block placement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.