Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
44.44% |
8 / 18 |
|
40.00% |
4 / 10 |
|
22.22% |
2 / 9 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LayoutBuilderEntityViewDisplay | |
44.44% |
8 / 18 |
|
40.00% |
4 / 10 |
|
22.22% |
2 / 9 |
|
33.33% |
1 / 3 |
30.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| preSave | |
50.00% |
2 / 4 |
|
50.00% |
3 / 6 |
|
16.67% |
1 / 6 |
|
0.00% |
0 / 1 |
13.26 | |||
| importFromLayoutBuilder | |
0.00% |
0 / 8 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_entity_view\Entity; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityStorageInterface; |
| 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\layout_builder\Entity\LayoutBuilderEntityViewDisplay as CoreLayoutBuilderEntityViewDisplay; |
| 13 | use Drupal\ui_patterns\Element\ComponentElementBuilder; |
| 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 activated, extends Layout Builder. |
| 20 | * |
| 21 | * @see \Drupal\display_builder_entity_view\Hook\DisplayBuilderEntityViewHook::entityTypeAlter() |
| 22 | * @see \Drupal\display_builder_entity_view\Entity\EntityViewDisplay |
| 23 | */ |
| 24 | class LayoutBuilderEntityViewDisplay extends CoreLayoutBuilderEntityViewDisplay implements DisplayBuilderEntityDisplayInterface { |
| 25 | |
| 26 | use EntityViewDisplayTrait; |
| 27 | |
| 28 | /** |
| 29 | * The source plugin manager. |
| 30 | */ |
| 31 | protected SourcePluginManager $sourcePluginManager; |
| 32 | |
| 33 | /** |
| 34 | * The entity type manager. |
| 35 | */ |
| 36 | protected EntityTypeManagerInterface $entityTypeManager; |
| 37 | |
| 38 | /** |
| 39 | * The component element builder service. |
| 40 | */ |
| 41 | protected ComponentElementBuilder $componentElementBuilder; |
| 42 | |
| 43 | /** |
| 44 | * The data converter from Manage Display and Layout Builder. |
| 45 | */ |
| 46 | protected BuilderDataConverter $dataConverter; |
| 47 | |
| 48 | /** |
| 49 | * The display buildable plugin manager. |
| 50 | */ |
| 51 | protected DisplayBuildablePluginManager $displayBuildableManager; |
| 52 | |
| 53 | /** |
| 54 | * The loaded display builder instance. |
| 55 | */ |
| 56 | protected ?InstanceInterface $instance; |
| 57 | |
| 58 | /** |
| 59 | * Constructs the LayoutBuilderEntityViewDisplay. |
| 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 | * {@inheritdoc} |
| 77 | */ |
| 78 | public function preSave(EntityStorageInterface $storage): void { |
| 79 | // If the update is made from Layout Builder, convert the data and copy |
| 80 | // to Display Builder's third party settings storage. |
| 81 | if (isset($this->form_id) && $this->form_id === 'entity_view_display_layout_builder_form') { |
| 82 | if ($this->displayBuildable()->getProfile()) { |
| 83 | $this->importFromLayoutBuilder(); |
| 84 | } |
| 85 | } |
| 86 | parent::preSave($storage); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Import and convert data from layout builder. |
| 91 | * |
| 92 | * This is not used for the first import but for the following saves. |
| 93 | * |
| 94 | * @see LayoutBuilderEntityViewDisplay::preSave() |
| 95 | */ |
| 96 | protected function importFromLayoutBuilder(): void { |
| 97 | if (!$this->displayBuildable()->getInstanceId()) { |
| 98 | return; |
| 99 | } |
| 100 | $sections = $this->getThirdPartySetting('layout_builder', 'sections'); |
| 101 | $sources = $this->dataConverter->convertFromLayoutBuilder($sections); |
| 102 | |
| 103 | $storage = $this->entityTypeManager->getStorage('display_builder_instance'); |
| 104 | /** @var \Drupal\display_builder\InstanceInterface $instance */ |
| 105 | $instance = $storage->load($this->displayBuildable()->getInstanceId()); |
| 106 | $instance->setNewPresent($sources, 'Import from Layout Builder'); |
| 107 | $instance->save(); |
| 108 | } |
| 109 | |
| 110 | } |