Skip to content

Commit e973fff

Browse files
authored
fix: proxy context-aware methods to target block (#34)
* fix: proxy context-aware methods to target block Override context methods on ProxyBlock to delegate to the target block, and warn in the form when context mapping is only available after save. * fix: avoid fatal runtime error * fix: drop unnecessary override * fix: not name * fix: definitions is plural * fix: use this
1 parent 92952d7 commit e973fff

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎src/Plugin/Block/ProxyBlock.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Drupal\proxy_block\Plugin\Block;
66

7+
use Drupal\Component\Plugin\Context\ContextInterface;
8+
use Drupal\Component\Plugin\Exception\ContextException;
79
use Drupal\Component\Utility\NestedArray;
810
use Drupal\Core\Block\Attribute\Block;
911
use Drupal\Core\Block\BlockBase;
@@ -295,4 +297,70 @@ public function getCacheMaxAge(): int {
295297
return $this->cacheManager->getCacheMaxAge($target_block, parent::getCacheMaxAge());
296298
}
297299

300+
/**
301+
* {@inheritdoc}
302+
*/
303+
public function getContext($name) {
304+
try {
305+
return parent::getContext($name);
306+
}
307+
catch (ContextException $e) {
308+
$target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration());
309+
if ($target_block instanceof ContextAwarePluginInterface) {
310+
return $target_block->getContext($name);
311+
}
312+
throw $e;
313+
}
314+
}
315+
316+
/**
317+
* {@inheritdoc}
318+
*/
319+
public function setContextMapping(array $context_mapping) {
320+
$target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration());
321+
if ($target_block instanceof ContextAwarePluginInterface) {
322+
$target_block->setContextMapping($context_mapping);
323+
}
324+
return parent::setContextMapping($context_mapping);
325+
}
326+
327+
/**
328+
* {@inheritdoc}
329+
*/
330+
public function setContext($name, ContextInterface $context) {
331+
$target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration());
332+
if ($target_block instanceof ContextAwarePluginInterface) {
333+
try {
334+
$target_block->setContext($name, $context);
335+
}
336+
catch (ContextException $e) {
337+
}
338+
}
339+
parent::setContext($name, $context);
340+
}
341+
342+
/**
343+
* {@inheritdoc}
344+
*/
345+
public function getContextDefinition($name) {
346+
$target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration());
347+
if ($target_block instanceof ContextAwarePluginInterface) {
348+
return $target_block->getContextDefinition($name);
349+
}
350+
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
351+
}
352+
353+
/**
354+
* {@inheritdoc}
355+
*/
356+
public function getContextDefinitions() {
357+
$target_block = $this->targetBlockFactory->getTargetBlock($this->getConfiguration());
358+
if (!$target_block) {
359+
return [];
360+
}
361+
return $target_block instanceof ContextAwarePluginInterface
362+
? $target_block->getContextDefinitions()
363+
: $target_block->getPluginDefinition()['context_definitions'] ?? [];
364+
}
365+
298366
}

‎src/Service/TargetBlockFormProcessor.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Drupal\proxy_block\Service;
66

7+
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
78
use Drupal\Core\Block\BlockPluginInterface;
89
use Drupal\Core\Form\FormState;
910
use Drupal\Component\Plugin\Exception\PluginException;
@@ -87,9 +88,23 @@ public function buildTargetBlockConfigurationForm(string $plugin_id, array $conf
8788
];
8889
}
8990

90-
if ($target_block instanceof ContextAwarePluginInterface) {
91-
$gathered_contexts = $this->contextManager->getGatheredContexts();
92-
$form_elements['context_mapping'] = $this->addContextAssignmentElement($target_block, $gathered_contexts);
91+
$target_definition = $target_block->getPluginDefinition();
92+
$context_definitions = $target_definition instanceof ContextAwarePluginDefinitionInterface
93+
? $target_definition->getContextDefinitions()
94+
: $target_definition['context_definitions'] ?? [];
95+
if (!empty($context_definitions)) {
96+
$form_elements['context_mapping'] = [
97+
'#theme' => 'status_messages',
98+
'#message_list' => [
99+
'warning' => [
100+
$this->t('This block needs additional settings (context mapping). See them below.'),
101+
$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.'),
102+
],
103+
],
104+
'#status_headings' => [
105+
'warning' => $this->t('Important'),
106+
],
107+
];
93108
}
94109

95110
return $form_elements;

0 commit comments

Comments
 (0)