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\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;
18 * Provides entity field block definitions for every field.
22 class FieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
24 use StringTranslationTrait;
27 * The entity type repository.
29 * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
31 protected $entityTypeRepository;
34 * The entity field manager.
36 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
38 protected $entityFieldManager;
41 * The field type manager.
43 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
45 protected $fieldTypeManager;
48 * The formatter manager.
50 * @var \Drupal\Core\Field\FormatterPluginManager
52 protected $formatterManager;
55 * Constructs new FieldBlockDeriver.
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.
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;
76 public static function create(ContainerInterface $container, $base_plugin_id) {
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')
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)) {
98 foreach ($field_info['bundles'] as $bundle) {
99 $derivative = $base_plugin_definition;
100 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle)[$field_name];
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'];
109 $derivative['category'] = $this->t('@entity fields', ['@entity' => $entity_type_labels[$entity_type_id]]);
111 $derivative['admin_label'] = $field_definition->getLabel();
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();
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');
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,
127 $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
128 $this->derivatives[$derivative_id] = $derivative;
132 return $this->derivatives;