"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/Plugin/Derivative/ExtraFieldBlockDeriver.php" (26 Nov 2020, 4406 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ExtraFieldBlockDeriver.php" see the Fossies "Dox" file reference documentation.

    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 use Drupal\Core\Entity\EntityTypeRepositoryInterface;
   16 
   17 /**
   18  * Provides entity field block definitions for every field.
   19  *
   20  * @internal
   21  *   Plugin derivers are internal.
   22  */
   23 class ExtraFieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
   24 
   25   use StringTranslationTrait;
   26 
   27   /**
   28    * The entity field manager.
   29    *
   30    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   31    */
   32   protected $entityFieldManager;
   33 
   34   /**
   35    * The entity type manager.
   36    *
   37    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   38    */
   39   protected $entityTypeManager;
   40 
   41   /**
   42    * The entity type bundle info.
   43    *
   44    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   45    */
   46   protected $entityTypeBundleInfo;
   47 
   48   /**
   49    * The entity type repository.
   50    *
   51    * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
   52    */
   53   protected $entityTypeRepository;
   54 
   55   /**
   56    * Constructs new FieldBlockDeriver.
   57    *
   58    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   59    *   The entity field manager.
   60    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   61    *   The entity type manager.
   62    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   63    *   The entity type bundle info.
   64    * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   65    *   The entity type repository.
   66    */
   67   public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeRepositoryInterface $entity_type_repository) {
   68     $this->entityFieldManager = $entity_field_manager;
   69     $this->entityTypeManager = $entity_type_manager;
   70     $this->entityTypeBundleInfo = $entity_type_bundle_info;
   71     $this->entityTypeRepository = $entity_type_repository;
   72   }
   73 
   74   /**
   75    * {@inheritdoc}
   76    */
   77   public static function create(ContainerInterface $container, $base_plugin_id) {
   78     return new static(
   79       $container->get('entity_field.manager'),
   80       $container->get('entity_type.manager'),
   81       $container->get('entity_type.bundle.info'),
   82       $container->get('entity_type.repository')
   83     );
   84   }
   85 
   86   /**
   87    * {@inheritdoc}
   88    */
   89   public function getDerivativeDefinitions($base_plugin_definition) {
   90     $entity_type_labels = $this->entityTypeRepository->getEntityTypeLabels();
   91     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
   92       // Only process fieldable entity types.
   93       if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
   94         continue;
   95       }
   96 
   97       $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
   98       foreach ($bundles as $bundle_id => $bundle) {
   99         $extra_fields = $this->entityFieldManager->getExtraFields($entity_type_id, $bundle_id);
  100         // Skip bundles without any extra fields.
  101         if (empty($extra_fields['display'])) {
  102           continue;
  103         }
  104 
  105         foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
  106           $derivative = $base_plugin_definition;
  107 
  108           $derivative['category'] = $this->t('@entity fields', ['@entity' => $entity_type_labels[$entity_type_id]]);
  109 
  110           $derivative['admin_label'] = $extra_field['label'];
  111 
  112           $context_definition = EntityContextDefinition::fromEntityType($entity_type)
  113             ->addConstraint('Bundle', [$bundle_id]);
  114           $derivative['context_definitions'] = [
  115             'entity' => $context_definition,
  116           ];
  117 
  118           $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle_id . PluginBase::DERIVATIVE_SEPARATOR . $extra_field_id;
  119           $this->derivatives[$derivative_id] = $derivative;
  120         }
  121       }
  122     }
  123     return $this->derivatives;
  124   }
  125 
  126 }