Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/Plugin/Block/ProxyBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Comment thread
e0ipso marked this conversation as resolved.
Resolved

/**
* {@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'] ?? [];
}
Comment thread
e0ipso marked this conversation as resolved.
Resolved

}
21 changes: 18 additions & 3 deletions src/Service/TargetBlockFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading