Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
38.10% |
8 / 21 |
|
25.00% |
2 / 8 |
|
33.33% |
2 / 6 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| EntityViewDisplay | |
38.10% |
8 / 21 |
|
25.00% |
2 / 8 |
|
33.33% |
2 / 6 |
|
66.67% |
2 / 3 |
12.41 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDisplayInfos | |
0.00% |
0 / 13 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| initialImport | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_entity_view\Entity; |
| 6 | |
| 7 | use Drupal\Core\Entity\Entity\EntityViewDisplay as CoreEntityViewDisplay; |
| 8 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 9 | use Drupal\Core\Extension\ModuleExtensionList; |
| 10 | use Drupal\display_builder\InstanceInterface; |
| 11 | use Drupal\display_builder_entity_view\BuilderDataConverter; |
| 12 | use Drupal\ui_patterns\Element\ComponentElementBuilder; |
| 13 | use Drupal\ui_patterns\Entity\SampleEntityGeneratorInterface; |
| 14 | use Drupal\ui_patterns\SourcePluginManager; |
| 15 | |
| 16 | /** |
| 17 | * Provides an entity view display entity that has a display builder. |
| 18 | * |
| 19 | * When Layout Builder is not activated, extends the default entity view |
| 20 | * display ("Manage display"). |
| 21 | * |
| 22 | * @see \Drupal\display_builder_entity_view\Hook\DisplayBuilderEntityViewHook::entityTypeAlter() |
| 23 | * @see \Drupal\display_builder_entity_view\Entity\LayoutBuilderEntityViewDisplay |
| 24 | */ |
| 25 | class EntityViewDisplay extends CoreEntityViewDisplay implements DisplayBuilderEntityDisplayInterface, DisplayBuilderOverridableInterface { |
| 26 | |
| 27 | use EntityViewDisplayTrait; |
| 28 | |
| 29 | /** |
| 30 | * The source plugin manager. |
| 31 | */ |
| 32 | protected SourcePluginManager $sourcePluginManager; |
| 33 | |
| 34 | /** |
| 35 | * The entity type manager. |
| 36 | */ |
| 37 | protected EntityTypeManagerInterface $entityTypeManager; |
| 38 | |
| 39 | /** |
| 40 | * The component element builder service. |
| 41 | */ |
| 42 | protected ComponentElementBuilder $componentElementBuilder; |
| 43 | |
| 44 | /** |
| 45 | * The sample entity generator. |
| 46 | */ |
| 47 | protected SampleEntityGeneratorInterface $sampleEntityGenerator; |
| 48 | |
| 49 | /** |
| 50 | * The list of modules. |
| 51 | */ |
| 52 | protected ModuleExtensionList $modules; |
| 53 | |
| 54 | /** |
| 55 | * The data converter from Manage Display and Layout Builder. |
| 56 | */ |
| 57 | protected BuilderDataConverter $dataConverter; |
| 58 | |
| 59 | /** |
| 60 | * The loaded display builder instance. |
| 61 | */ |
| 62 | protected ?InstanceInterface $instance; |
| 63 | |
| 64 | /** |
| 65 | * Constructs the EntityViewDisplay. |
| 66 | * |
| 67 | * @param array $values |
| 68 | * The values to initialize the entity with. |
| 69 | * @param string $entity_type |
| 70 | * The entity type ID. |
| 71 | */ |
| 72 | public function __construct(array $values, $entity_type) { |
| 73 | parent::__construct($values, $entity_type); |
| 74 | $this->sourcePluginManager = \Drupal::service('plugin.manager.ui_patterns_source'); |
| 75 | $this->entityTypeManager = \Drupal::service('entity_type.manager'); |
| 76 | $this->componentElementBuilder = \Drupal::service('ui_patterns.component_element_builder'); |
| 77 | $this->sampleEntityGenerator = \Drupal::service('ui_patterns.sample_entity_generator'); |
| 78 | $this->modules = \Drupal::service('extension.list.module'); |
| 79 | $this->dataConverter = \Drupal::service('display_builder_entity_view.builder_data_converter'); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Gets entity_view_display information grouped by entity type. |
| 84 | * |
| 85 | * @todo To remove. |
| 86 | * https://www.drupal.org/project/display_builder/issues/3542273 |
| 87 | * |
| 88 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
| 89 | * The entity type manager service. |
| 90 | * |
| 91 | * @return array |
| 92 | * An array of display information keyed with 'node', then 'modes' and |
| 93 | * 'bundles': |
| 94 | * |
| 95 | * @code |
| 96 | * [ |
| 97 | * 'node' => [ |
| 98 | * 'modes' => [ |
| 99 | * 'teaser' => 'Teaser', |
| 100 | * ], |
| 101 | * 'bundles' => [ |
| 102 | * 'article' => [ |
| 103 | * 'teaser' => 'Teaser', |
| 104 | * ], |
| 105 | * ], |
| 106 | * ], |
| 107 | * ]; |
| 108 | * |
| 109 | * @endcode |
| 110 | */ |
| 111 | public static function getDisplayInfos(EntityTypeManagerInterface $entityTypeManager): array { |
| 112 | /** @var \Drupal\display_builder_entity_view\Entity\EntityViewDisplay[] $displays */ |
| 113 | $displays = $entityTypeManager |
| 114 | ->getStorage('entity_view_display') |
| 115 | ->loadMultiple(); |
| 116 | $view_mode_storage = $entityTypeManager->getStorage('entity_view_mode'); |
| 117 | $tabs_info = []; |
| 118 | |
| 119 | foreach ($displays as $display) { |
| 120 | if (!$display->getDisplayBuilderOverrideField()) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | $entity_type_id = $display->getTargetEntityTypeId(); |
| 125 | $view_mode = $view_mode_storage->load(\sprintf('%s.%s', $entity_type_id, $display->getMode())); |
| 126 | $tabs_info[$entity_type_id]['modes'][$display->getMode()] = $view_mode?->label() ?? t('Default'); |
| 127 | $tabs_info[$entity_type_id]['bundles'][$display->getTargetBundle()][$display->getMode()] = $view_mode?->label() ?? t('Default'); |
| 128 | } |
| 129 | |
| 130 | return $tabs_info; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Initial import from existing data. |
| 135 | * |
| 136 | * @return array |
| 137 | * List of UI Patterns sources. |
| 138 | * |
| 139 | * @see EntityViewDisplayTrait::initInstanceIfMissing() |
| 140 | * @see LayoutBuilderEntityViewDisplay::initialImport() |
| 141 | */ |
| 142 | protected function initialImport(): array { |
| 143 | return $this->dataConverter->convertFromManageDisplay($this->getTargetEntityTypeId(), $this->getTargetBundle(), $this->content); |
| 144 | } |
| 145 | |
| 146 | } |