Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| BuilderDataConverter | |
0.00% |
0 / 42 |
|
0.00% |
0 / 15 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| convertPage | |
0.00% |
0 / 23 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| convertBlock | |
0.00% |
0 / 18 |
|
0.00% |
0 / 5 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_page_layout; |
| 6 | |
| 7 | use Drupal\block\BlockRepositoryInterface; |
| 8 | use Drupal\Core\Block\BlockPluginInterface; |
| 9 | use Drupal\Core\Config\ConfigFactoryInterface; |
| 10 | use Drupal\Core\Theme\ThemeInitializationInterface; |
| 11 | use Drupal\Core\Theme\ThemeManagerInterface; |
| 12 | |
| 13 | /** |
| 14 | * Convert data between page regions and Display Builder. |
| 15 | */ |
| 16 | class BuilderDataConverter { |
| 17 | |
| 18 | public function __construct( |
| 19 | private BlockRepositoryInterface $blockRepository, |
| 20 | private ThemeManagerInterface $themeManager, |
| 21 | private ThemeInitializationInterface $themeInitialization, |
| 22 | private ConfigFactoryInterface $configFactory, |
| 23 | ) {} |
| 24 | |
| 25 | /** |
| 26 | * Convert page regions to UI Patterns sources. |
| 27 | * |
| 28 | * @return array |
| 29 | * A single UI Patterns source data. |
| 30 | */ |
| 31 | public function convertPage(): array { |
| 32 | $sources = [ |
| 33 | 'source_id' => 'page_layout', |
| 34 | 'source' => [ |
| 35 | 'regions' => [], |
| 36 | ], |
| 37 | ]; |
| 38 | |
| 39 | // Current theme is probably the admin one, so let's switch to the front |
| 40 | // one before retrieving the blocks. |
| 41 | $current_theme = $this->themeManager->getActiveTheme(); |
| 42 | $theme_name = $this->configFactory->get('system.theme')->get('default'); |
| 43 | $theme = $this->themeInitialization->getActiveThemeByName($theme_name); |
| 44 | $this->themeManager->setActiveTheme($theme); |
| 45 | |
| 46 | foreach ($this->blockRepository->getVisibleBlocksPerRegion() as $region => $blocks) { |
| 47 | foreach ($blocks as $block) { |
| 48 | $source = $this->convertBlock($block->getPlugin()); |
| 49 | |
| 50 | // Remove config related to the block config entity and keep only the |
| 51 | // properties from the plugin itself. |
| 52 | if ($block_id = $source['source']['plugin_id'] ?? NULL) { |
| 53 | unset( |
| 54 | $source['source'][$block_id]['id'], |
| 55 | $source['source'][$block_id]['label'], |
| 56 | $source['source'][$block_id]['label_display'], |
| 57 | $source['source'][$block_id]['provider'], |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | $sources['source']['regions'][$region][] = $source; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Restore default theme after rendering. |
| 66 | $this->themeManager->setActiveTheme($current_theme); |
| 67 | |
| 68 | return $sources; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Convert block plugins. |
| 73 | * |
| 74 | * @param \Drupal\Core\Block\BlockPluginInterface $block |
| 75 | * The block plugin to convert. |
| 76 | * |
| 77 | * @return array |
| 78 | * A single UI Patterns source data. |
| 79 | */ |
| 80 | private function convertBlock(BlockPluginInterface $block): array { |
| 81 | $block_id = $block->getPluginId(); |
| 82 | |
| 83 | if ($block_id === 'system_main_block') { |
| 84 | return [ |
| 85 | 'source_id' => 'main_page_content', |
| 86 | 'source' => [], |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | if ($block_id === 'page_title_block') { |
| 91 | return [ |
| 92 | 'source_id' => 'page_title', |
| 93 | 'source' => [], |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | return [ |
| 98 | 'source_id' => 'block', |
| 99 | 'source' => [ |
| 100 | 'plugin_id' => $block_id, |
| 101 | $block_id => $block->getConfiguration(), |
| 102 | ], |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | } |