FieldBlockDeriver.php in Drupal 10
File
core/modules/layout_builder/src/Plugin/Derivative/FieldBlockDeriver.php
View source
<?php
namespace Drupal\layout_builder\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeRepository;
protected $entityFieldManager;
protected $fieldTypeManager;
protected $formatterManager;
public function __construct(EntityTypeRepositoryInterface $entity_type_repository, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) {
$this->entityTypeRepository = $entity_type_repository;
$this->entityFieldManager = $entity_field_manager;
$this->fieldTypeManager = $field_type_manager;
$this->formatterManager = $formatter_manager;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.repository'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('plugin.manager.field.formatter'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$entity_type_labels = $this->entityTypeRepository
->getEntityTypeLabels();
foreach ($this->entityFieldManager
->getFieldMap() as $entity_type_id => $entity_field_map) {
foreach ($entity_field_map as $field_name => $field_info) {
$options = $this->formatterManager
->getOptions($field_info['type']);
if (empty($options)) {
continue;
}
foreach ($field_info['bundles'] as $bundle) {
$derivative = $base_plugin_definition;
$field_definition = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle)[$field_name];
$derivative['default_formatter'] = '';
$field_type_definition = $this->fieldTypeManager
->getDefinition($field_info['type']);
if (isset($field_type_definition['default_formatter'])) {
$derivative['default_formatter'] = $field_type_definition['default_formatter'];
}
$derivative['category'] = $this
->t('@entity fields', [
'@entity' => $entity_type_labels[$entity_type_id],
]);
$derivative['admin_label'] = $field_definition
->getLabel();
if ($field_definition instanceof FieldConfigInterface) {
$derivative['config_dependencies'][$field_definition
->getConfigDependencyKey()][] = $field_definition
->getConfigDependencyName();
}
$derivative['_block_ui_hidden'] = !$field_definition
->isDisplayConfigurable('view');
$context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)
->setLabel($entity_type_labels[$entity_type_id]);
$context_definition
->addConstraint('Bundle', [
$bundle,
]);
$derivative['context_definitions'] = [
'entity' => $context_definition,
'view_mode' => new ContextDefinition('string'),
];
$derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
$this->derivatives[$derivative_id] = $derivative;
}
}
}
return $this->derivatives;
}
}