Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / layout_builder / src / Plugin / Derivative / ExtraFieldBlockDeriver.php
1 <?php
2
3 namespace Drupal\layout_builder\Plugin\Derivative;
4
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;
15
16 /**
17  * Provides entity field block definitions for every field.
18  *
19  * @internal
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.
23  */
24 class ExtraFieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
25
26   use StringTranslationTrait;
27
28   /**
29    * The entity field manager.
30    *
31    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
32    */
33   protected $entityFieldManager;
34
35   /**
36    * The entity type manager.
37    *
38    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39    */
40   protected $entityTypeManager;
41
42   /**
43    * The entity type bundle info.
44    *
45    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
46    */
47   protected $entityTypeBundleInfo;
48
49   /**
50    * Constructs new FieldBlockDeriver.
51    *
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.
58    */
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;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public static function create(ContainerInterface $container, $base_plugin_id) {
69     return new static(
70       $container->get('entity_field.manager'),
71       $container->get('entity_type.manager'),
72       $container->get('entity_type.bundle.info')
73     );
74   }
75
76   /**
77    * {@inheritdoc}
78    */
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)) {
83         continue;
84       }
85
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'])) {
91           continue;
92         }
93
94         foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
95           $derivative = $base_plugin_definition;
96
97           $derivative['category'] = $entity_type->getLabel();
98
99           $derivative['admin_label'] = $extra_field['label'];
100
101           $context_definition = EntityContextDefinition::fromEntityType($entity_type)
102             ->addConstraint('Bundle', [$bundle_id]);
103           $derivative['context'] = [
104             'entity' => $context_definition,
105           ];
106
107           $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle_id . PluginBase::DERIVATIVE_SEPARATOR . $extra_field_id;
108           $this->derivatives[$derivative_id] = $derivative;
109         }
110       }
111     }
112     return $this->derivatives;
113   }
114
115 }