PdbBlockDeriver.php in Decoupled Blocks 8
File
src/Plugin/Derivative/PdbBlockDeriver.php
View source
<?php
namespace Drupal\pdb\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\pdb\ComponentDiscoveryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PdbBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
protected $componentDiscovery;
public function __construct(ComponentDiscoveryInterface $component_discovery) {
$this->componentDiscovery = $component_discovery;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('pdb.component_discovery'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$components = $this->componentDiscovery
->getComponents();
foreach ($components as $block_id => $block_info) {
$this->derivatives[$block_id] = $base_plugin_definition;
$this->derivatives[$block_id]['info'] = $block_info->info;
$this->derivatives[$block_id]['admin_label'] = $block_info->info['name'];
$this->derivatives[$block_id]['cache'] = [
'max-age' => 0,
];
if (isset($block_info->info['category'])) {
$this->derivatives[$block_id]['category'] = $block_info->info['category'];
}
if (isset($block_info->info['contexts'])) {
$this->derivatives[$block_id]['context_definitions'] = $this
->createContexts($block_info->info['contexts']);
}
}
return $this->derivatives;
}
protected function createContexts(array $contexts) {
$contexts_definitions = [];
if (isset($contexts['entity']) && $contexts['entity'] === 'node') {
$contexts['entity'] = 'entity:node';
}
foreach ($contexts as $context_id => $context_type) {
if (strpos($context_type, 'entity:') === 0) {
$contexts_definitions[$context_id] = new EntityContextDefinition($context_type);
}
else {
$contexts_definitions[$context_id] = new ContextDefinition($context_type);
}
}
return $contexts_definitions;
}
}