Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / src / Plugin / Derivative / FieldBlockDeriver.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\EntityTypeRepositoryInterface;
9 use Drupal\Core\Field\FieldConfigInterface;
10 use Drupal\Core\Field\FieldTypePluginManagerInterface;
11 use Drupal\Core\Field\FormatterPluginManager;
12 use Drupal\Core\Plugin\Context\EntityContextDefinition;
13 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
14 use Drupal\Core\StringTranslation\StringTranslationTrait;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Provides entity field block definitions for every field.
19  *
20  * @internal
21  */
22 class FieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
23
24   use StringTranslationTrait;
25
26   /**
27    * The entity type repository.
28    *
29    * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
30    */
31   protected $entityTypeRepository;
32
33   /**
34    * The entity field manager.
35    *
36    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
37    */
38   protected $entityFieldManager;
39
40   /**
41    * The field type manager.
42    *
43    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
44    */
45   protected $fieldTypeManager;
46
47   /**
48    * The formatter manager.
49    *
50    * @var \Drupal\Core\Field\FormatterPluginManager
51    */
52   protected $formatterManager;
53
54   /**
55    * Constructs new FieldBlockDeriver.
56    *
57    * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
58    *   The entity type repository.
59    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
60    *   The entity field manager.
61    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
62    *   The field type manager.
63    * @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager
64    *   The formatter manager.
65    */
66   public function __construct(EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) {
67     $this->entityTypeRepository = $entity_type_repository;
68     $this->entityFieldManager = $entity_field_manager;
69     $this->fieldTypeManager = $field_type_manager;
70     $this->formatterManager = $formatter_manager;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public static function create(ContainerInterface $container, $base_plugin_id) {
77     return new static(
78       $container->get('entity_type.repository'),
79       $container->get('entity_field.manager'),
80       $container->get('plugin.manager.field.field_type'),
81       $container->get('plugin.manager.field.formatter')
82     );
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getDerivativeDefinitions($base_plugin_definition) {
89     $entity_type_labels = $this->entityTypeRepository->getEntityTypeLabels();
90     foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) {
91       foreach ($entity_field_map as $field_name => $field_info) {
92         // Skip fields without any formatters.
93         $options = $this->formatterManager->getOptions($field_info['type']);
94         if (empty($options)) {
95           continue;
96         }
97
98         foreach ($field_info['bundles'] as $bundle) {
99           $derivative = $base_plugin_definition;
100           $field_definition = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle)[$field_name];
101
102           // Store the default formatter on the definition.
103           $derivative['default_formatter'] = '';
104           $field_type_definition = $this->fieldTypeManager->getDefinition($field_info['type']);
105           if (isset($field_type_definition['default_formatter'])) {
106             $derivative['default_formatter'] = $field_type_definition['default_formatter'];
107           }
108
109           $derivative['category'] = $this->t('@entity', ['@entity' => $entity_type_labels[$entity_type_id]]);
110
111           $derivative['admin_label'] = $field_definition->getLabel();
112
113           // Add a dependency on the field if it is configurable.
114           if ($field_definition instanceof FieldConfigInterface) {
115             $derivative['config_dependencies'][$field_definition->getConfigDependencyKey()][] = $field_definition->getConfigDependencyName();
116           }
117           // For any field that is not display configurable, mark it as
118           // unavailable to place in the block UI.
119           $derivative['_block_ui_hidden'] = !$field_definition->isDisplayConfigurable('view');
120
121           $context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)->setLabel($entity_type_labels[$entity_type_id]);
122           $context_definition->addConstraint('Bundle', [$bundle]);
123           $derivative['context'] = [
124             'entity' => $context_definition,
125           ];
126
127           $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
128           $this->derivatives[$derivative_id] = $derivative;
129         }
130       }
131     }
132     return $this->derivatives;
133   }
134
135 }