class EntityDisplayRebuilder

Same name and namespace in other branches
  1. 11.x core/modules/field/src/EntityDisplayRebuilder.php \Drupal\field\EntityDisplayRebuilder
  2. 10 core/modules/field/src/EntityDisplayRebuilder.php \Drupal\field\EntityDisplayRebuilder
  3. 9 core/modules/field/src/EntityDisplayRebuilder.php \Drupal\field\EntityDisplayRebuilder
  4. 8.9.x core/modules/field/src/EntityDisplayRebuilder.php \Drupal\field\EntityDisplayRebuilder

Rebuilds all form and view modes for a passed entity bundle.

@internal

Hierarchy

Expanded class hierarchy of EntityDisplayRebuilder

See also

\Drupal\field\Hook\FieldHooks::fieldConfigInsert()

1 file declares its use of EntityDisplayRebuilder
FieldHooks.php in core/modules/field/src/Hook/FieldHooks.php

File

core/modules/field/src/EntityDisplayRebuilder.php, line 18

Namespace

Drupal\field
View source
class EntityDisplayRebuilder implements ContainerInjectionInterface {
  
  /**
   * Constructs a new EntityDisplayRebuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
   *   The entity display repository.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
   *   The entity type bundle info.
   */
  public function __construct(protected EntityTypeManagerInterface $entityTypeManager, protected EntityDisplayRepositoryInterface $entityDisplayRepository, protected EntityTypeBundleInfoInterface $entityTypeBundleInfo) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('entity_type.manager'), $container->get('entity_display.repository'), $container->get('entity_type.bundle.info'));
  }
  
  /**
   * Rebuild displays for single Entity Type.
   *
   * @param string $entity_type_id
   *   The entity type machine name.
   * @param string $bundle
   *   The bundle we need to rebuild.
   */
  public function rebuildEntityTypeDisplays($entity_type_id, $bundle) {
    // Get the displays.
    $view_modes = $this->entityDisplayRepository
      ->getViewModeOptions($entity_type_id);
    $form_modes = $this->entityDisplayRepository
      ->getFormModeOptions($entity_type_id);
    // Save view mode displays.
    $view_mode_ids = array_map(function ($view_mode) use ($entity_type_id, $bundle) {
      return "{$entity_type_id}.{$bundle}.{$view_mode}";
    }, array_keys($view_modes));
    foreach ($this->entityTypeManager
      ->getStorage('entity_view_display')
      ->loadMultiple($view_mode_ids) as $display) {
      $display->save();
    }
    // Save form mode displays.
    $form_mode_ids = array_map(function ($form_mode) use ($entity_type_id, $bundle) {
      return "{$entity_type_id}.{$bundle}.{$form_mode}";
    }, array_keys($form_modes));
    foreach ($this->entityTypeManager
      ->getStorage('entity_form_display')
      ->loadMultiple($form_mode_ids) as $display) {
      $display->save();
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityDisplayRebuilder::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityDisplayRebuilder::rebuildEntityTypeDisplays public function Rebuild displays for single Entity Type.
EntityDisplayRebuilder::__construct public function Constructs a new EntityDisplayRebuilder.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.