ComponentBlockBlockDeriver.php in Component blocks 1.0.x
File
src/Plugin/Deriver/ComponentBlockBlockDeriver.php
View source
<?php
namespace Drupal\component_blocks\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ComponentBlockBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
private $pluginManager;
private $entityTypeManager;
public function __construct(UiPatternsManager $pluginManager, EntityTypeManagerInterface $entityTypeManager) {
$this->pluginManager = $pluginManager;
$this->entityTypeManager = $entityTypeManager;
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->pluginManager
->getDefinitions() as $id => $definition) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_definition) {
if (!$entity_type_definition
->entityClassImplements(ContentEntityInterface::class)) {
continue;
}
$entity_type_id = $entity_type_definition
->id();
$entity_type_label = $entity_type_definition
->getLabel();
$context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)
->setLabel($entity_type_label);
$this->derivatives[$entity_type_id . ':' . $id] = [
'admin_label' => new TranslatableMarkup('@component with fields from @entity', [
'@component' => $definition['label'],
'@entity' => $entity_type_label,
]),
'_block_ui_hidden' => TRUE,
'ui_pattern_id' => $id,
'context_definitions' => [
'entity' => $context_definition,
],
] + $base_plugin_definition;
}
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('plugin.manager.ui_patterns'), $container
->get('entity_type.manager'));
}
}