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