diff --git a/src/Plugin/Block/ProxyBlock.php b/src/Plugin/Block/ProxyBlock.php index 4f807aa..c241772 100644 --- a/src/Plugin/Block/ProxyBlock.php +++ b/src/Plugin/Block/ProxyBlock.php @@ -4,6 +4,8 @@ namespace Drupal\proxy_block\Plugin\Block; +use Drupal\Component\Plugin\Context\ContextInterface; +use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; @@ -295,4 +297,70 @@ public function getCacheMaxAge(): int { return $this->cacheManager->getCacheMaxAge($target_block, parent::getCacheMaxAge()); } + /** + * {@inheritdoc} + */ + public function getContext($name) { + try { + return parent::getContext($name); + } + catch (ContextException $e) { + $target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration()); + if ($target_block instanceof ContextAwarePluginInterface) { + return $target_block->getContext($name); + } + throw $e; + } + } + + /** + * {@inheritdoc} + */ + public function setContextMapping(array $context_mapping) { + $target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration()); + if ($target_block instanceof ContextAwarePluginInterface) { + $target_block->setContextMapping($context_mapping); + } + return parent::setContextMapping($context_mapping); + } + + /** + * {@inheritdoc} + */ + public function setContext($name, ContextInterface $context) { + $target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration()); + if ($target_block instanceof ContextAwarePluginInterface) { + try { + $target_block->setContext($name, $context); + } + catch (ContextException $e) { + } + } + parent::setContext($name, $context); + } + + /** + * {@inheritdoc} + */ + public function getContextDefinition($name) { + $target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration()); + if ($target_block instanceof ContextAwarePluginInterface) { + return $target_block->getContextDefinition($name); + } + throw new ContextException(sprintf("The %s context is not a valid context.", $name)); + } + + /** + * {@inheritdoc} + */ + public function getContextDefinitions() { + $target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration()); + if (!$target_block) { + return []; + } + return $target_block instanceof ContextAwarePluginInterface + ? $target_block->getContextDefinitions() + : $target_block->getPluginDefinition()['context_definitions'] ?? []; + } + } diff --git a/src/Service/TargetBlockFormProcessor.php b/src/Service/TargetBlockFormProcessor.php index dfcdc70..fd2fe9b 100644 --- a/src/Service/TargetBlockFormProcessor.php +++ b/src/Service/TargetBlockFormProcessor.php @@ -4,6 +4,7 @@ namespace Drupal\proxy_block\Service; +use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Form\FormState; use Drupal\Component\Plugin\Exception\PluginException; @@ -87,9 +88,23 @@ public function buildTargetBlockConfigurationForm(string $plugin_id, array $conf ]; } - if ($target_block instanceof ContextAwarePluginInterface) { - $gathered_contexts = $this->contextManager->getGatheredContexts(); - $form_elements['context_mapping'] = $this->addContextAssignmentElement($target_block, $gathered_contexts); + $target_definition = $target_block->getPluginDefinition(); + $context_definitions = $target_definition instanceof ContextAwarePluginDefinitionInterface + ? $target_definition->getContextDefinitions() + : $target_definition['context_definitions'] ?? []; + if (!empty($context_definitions)) { + $form_elements['context_mapping'] = [ + '#theme' => 'status_messages', + '#message_list' => [ + 'warning' => [ + $this->t('This block needs additional settings (context mapping). See them below.'), + $this->t('If you are just adding the block, the additional settings are not available. Save the block, and then update it to see the additional settings.'), + ], + ], + '#status_headings' => [ + 'warning' => $this->t('Important'), + ], + ]; } return $form_elements;