Resolve #3498248 "Pagetemplate exposed regions"
Closes #3498248
Merge request reports
Activity
requested review from @effulgentsia and @lauriii
assigned to @wimleers
added 1 commit
- 3530cd84 - Now make it work both when sending layout data to the client and when previewing.
added 4 commits
Toggle commit listrequested review from @f.mazeikis
added 6 commits
-
03f9c6d3...7af78a86 - 5 commits from branch
project:0.x
- f6bf8c14 - Merge remote-tracking branch 'origin/0.x' into 3498248-pagetemplate-exposed-regions
-
03f9c6d3...7af78a86 - 5 commits from branch
added 1 commit
requested review from @effulgentsia and removed approval
added 1 commit
- Resolved by Lee Rowlands
- Resolved by Lee Rowlands
- Resolved by Wim Leers
- Resolved by Wim Leers
- Resolved by Wim Leers
- Resolved by Wim Leers
- Resolved by Wim Leers
148 151 149 152 private function addGlobalRegions(PageTemplate $template, array &$model, array &$layout): void { 153 $active_component_trees = iterator_to_array($template->getComponentTrees()); 154 // Only expose regions marked as editable in the `layout` for the client. 155 // @see experience_builder_form_system_theme_settings_alter() 156 $editable_regions = array_keys(array_filter($template->get('editable'))); 157 150 158 $draft_template = $this->autoSaveManager->getAutoSaveData($template); 151 159 if ($draft_template === NULL) { 152 foreach ($template->getComponentTrees() as $region => $tree) { 160 foreach ($editable_regions as $region) { 153 161 if ($region === 'content') { 154 162 continue; 155 163 } 156 $layout[] = $this->buildRegion($region, $tree, $model); 164 $layout[] = $this->buildRegion($region, $active_component_trees[$region] ?? NULL, $model); Added
PageTemplate::getEditableRegions()
: f3040d1b.
171 // editable regions. 172 $draft_layout_region_nodes = array_filter($draft_template['layout'], fn (array $layout_node): bool => $layout_node['nodeType'] === 'region'); 173 $autosaved_regions = array_column($draft_layout_region_nodes, 'id'); 174 $missing_regions_in_auto_save = array_diff($editable_regions, $autosaved_regions); 175 foreach ($missing_regions_in_auto_save as $region) { 176 if ($region === 'content') { 177 continue; 178 } 179 $layout[] = $this->buildRegion($region, $active_component_trees[$region] ?? NULL, $model); 180 } 181 $extraneous_regions_in_auto_save = array_diff($autosaved_regions, $editable_regions); 182 foreach ($extraneous_regions_in_auto_save as $region) { 183 foreach ($draft_template['layout'] as $index => $region_node) { 184 if ($region_node['id'] === $region) { 185 unset($draft_template['layout'][$index]); 186 // @todo In principle, $model should be updated too, to omit props for components in the omitted regions. There's no consequences yet for not doing that though. - Resolved by Wim Leers
41 41 if ($preview && $autoSaveData = $this->autoSaveManager->getAutoSaveData($page_template)) { 42 42 // Generate a new template based on the auto-saved data. 43 43 try { 44 $page_template = $page_template->forAutoSaveData($autoSaveData)->enable(); 44 $autosaved_page_template = $page_template->forAutoSaveData($autoSaveData)->enable(); 45 // There are fewer region component trees auto-saved than there are 46 // regions when some of them are marked as not editable. For the regions 47 // that cannot be edited: load them from the active page template. 48 // @see experience_builder_form_system_theme_settings_alter() 49 if (count($autosaved_page_template->get('component_trees')) < count($page_template->get('editable'))) { 50 $autosaved_page_template->set('component_trees', 51 $autosaved_page_template->get('component_trees') 52 + 53 $page_template->get('component_trees') changed this line in version 19 of the diff
You're on to something!
First thought: disagreement
I think it's reasonable to use the "raw" information here, because AFAIK the entire auto-save functionality is done in a way that might change? The
$autosaved_page_template
config entity is not valid, because it doesn't contain information for all theme regions, which is required!So, I refactored the code to show that: 0154a9c6
Second thought: connecting dots
As I did 0154a9c6, I noticed that … this code is literally in a
try … catch
for catchingConstraintViolationException
, so … naturally the logic I wrote here should be insidePageTemplate::forAutoSaveData()
! But worse: why wasn't this already throwing an exception?!?!Turns out it was only performing validation for each region's component tree, instead of … just validating the entire
PageTemplate
- To make this more apparent, I changed its test coverage to explicitly assert validation errors: b0a0ff85.
- That in turn made it easy to prove that in its current state, invalid
PageTemplate
config entities are returned: e7480d62. - Finally, I was able to move the logic you're (rightfully!
) questioning here into::forAutoSaveData()
: 9b92645e
Conclusion
- I failed to see that I should've updated
PageTemplate::forAutoSaveData()
-
PageTemplate::forAutoSaveData()
was buggy, because it was validating only parts. That likely would've helped me prevent 1.
Fixed in the aforementioned commits, and in particular in 9b92645e. Auto-saving support for
PageTemplate
s is now way more robust!… and having gotten all that in place, I was then able to refactor it to be way simpler: e369eb03
:
146 146 if ($region === 'content') { 147 147 continue; 148 148 } 149 // Do not wrap regions marked as not editable; this causes visible whitespace. 150 // @see experience_builder_form_system_theme_settings_alter() 151 if ($page_template->get('editable')[$region] === FALSE) { 152 continue; 153 } - Comment on lines +151 to +153
added 1 commit
added 1 commit
added 1 commit
added 1 commit
- 62d95848 - The automatic fallback to the (guaranteed-to-be-valid) active/stored...
added 29 commits
-
f2057353 - 1 commit from branch
project:0.x
- f2057353...72545fb0 - 18 earlier commits
- 357e780c - Fix assertion.
- 2f774831 - @larowlan review: Show that `$page_template->forAutoSaveData($autoSaveData)`...
- 20c895ad - Refactor `PageTemplateValidationTest::testInvalidAutoSave()` to make the...
- 996403a5 - Prove beyond any doubt that `PageTemplate::forAutoSaveData()` can return...
- 95d2f0ed - Fix indirectly related bug that gets in the way here: when `$server_props` is...
- cb4f40c9 - `phpcs` + `phpstan`
- b492e23f - @larowlan review: move the most complex bit of new logic into `PageTemplate::forAutoSaveData()`.
- 81b33ff4 - The automatic fallback to the (guaranteed-to-be-valid) active/stored...
-
4f6d135e - Self-review: refactor `::forAutoSaveData()` to be WAY simpler now!
- 9f556c0c - Clarifications/clean-up.
Toggle commit list-
f2057353 - 1 commit from branch