3 namespace Drupal\layout_builder\Plugin\Derivative;
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Component\Plugin\PluginBase;
7 use Drupal\Core\Entity\EntityFieldManagerInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Entity\FieldableEntityInterface;
11 use Drupal\Core\Plugin\Context\EntityContextDefinition;
12 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
13 use Drupal\Core\StringTranslation\StringTranslationTrait;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Provides entity field block definitions for every field.
20 * Layout Builder is currently experimental and should only be leveraged by
21 * experimental modules and development releases of contributed modules.
22 * See https://www.drupal.org/core/experimental for more information.
24 class ExtraFieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
26 use StringTranslationTrait;
29 * The entity field manager.
31 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
33 protected $entityFieldManager;
36 * The entity type manager.
38 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40 protected $entityTypeManager;
43 * The entity type bundle info.
45 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
47 protected $entityTypeBundleInfo;
50 * Constructs new FieldBlockDeriver.
52 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
53 * The entity field manager.
54 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
55 * The entity type manager.
56 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
57 * The entity type bundle info.
59 public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
60 $this->entityFieldManager = $entity_field_manager;
61 $this->entityTypeManager = $entity_type_manager;
62 $this->entityTypeBundleInfo = $entity_type_bundle_info;
68 public static function create(ContainerInterface $container, $base_plugin_id) {
70 $container->get('entity_field.manager'),
71 $container->get('entity_type.manager'),
72 $container->get('entity_type.bundle.info')
79 public function getDerivativeDefinitions($base_plugin_definition) {
80 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
81 // Only process fieldable entity types.
82 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
86 $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
87 foreach ($bundles as $bundle_id => $bundle) {
88 $extra_fields = $this->entityFieldManager->getExtraFields($entity_type_id, $bundle_id);
89 // Skip bundles without any extra fields.
90 if (empty($extra_fields['display'])) {
94 foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
95 $derivative = $base_plugin_definition;
97 $derivative['category'] = $entity_type->getLabel();
99 $derivative['admin_label'] = $extra_field['label'];
101 $context_definition = EntityContextDefinition::fromEntityType($entity_type)
102 ->addConstraint('Bundle', [$bundle_id]);
103 $derivative['context'] = [
104 'entity' => $context_definition,
107 $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle_id . PluginBase::DERIVATIVE_SEPARATOR . $extra_field_id;
108 $this->derivatives[$derivative_id] = $derivative;
112 return $this->derivatives;