Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions web/themes/contrib/civictheme/civictheme.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,73 @@ function civictheme_post_update_remove_civictheme_iframe_field_c_p_attributes():

return (string) new TranslatableMarkup("Removed civictheme_iframe field 'field_c_p_attributes' instance and storage if they existed.");
}

/**
* Migrate deprecated layouts to civictheme_three_columns.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.StaticAccess)
*/
function civictheme_post_update_migrate_one_column_layouts(): string {
$outdated_layouts = [
'civictheme_one_column',
'civictheme_one_column_contained',
];

$messages = [];
$entity_displays = LayoutBuilderEntityViewDisplay::loadMultiple();
$updated_entity_displays = [];
foreach ($entity_displays as $entity_display) {
if (!$entity_display->isLayoutBuilderEnabled()) {
continue;
}
// Update allowed layouts if the deprecated ones are present.
$entity_view_mode_restriction = $entity_display->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction');
if (!empty($entity_view_mode_restriction['allowed_layouts'])) {
$allowed_layouts = $entity_view_mode_restriction['allowed_layouts'];
$replaced = FALSE;
foreach ($allowed_layouts as $idx => $layout_name) {
if (in_array($layout_name, $outdated_layouts)) {
unset($allowed_layouts[$idx]);
$replaced = TRUE;
}
}
if ($replaced) {
$allowed_layouts[] = 'civictheme_three_columns';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Prevent potential duplicate entry in allowed_layouts.

Before appending 'civictheme_three_columns', verify it doesn't already exist in $allowed_layouts to maintain idempotency and prevent duplicates.

Apply this diff:

       if ($replaced) {
-        $allowed_layouts[] = 'civictheme_three_columns';
+        if (!in_array('civictheme_three_columns', $allowed_layouts, TRUE)) {
+          $allowed_layouts[] = 'civictheme_three_columns';
+        }
         $entity_view_mode_restriction['allowed_layouts'] = array_values($allowed_layouts);
🤖 Prompt for AI Agents
In web/themes/contrib/civictheme/civictheme.post_update.php around line 1022,
the code unconditionally appends 'civictheme_three_columns' to $allowed_layouts
which can create duplicate entries; update the code to check if
'civictheme_three_columns' is not already present in $allowed_layouts (e.g.,
using in_array or array_search) before appending so the operation is idempotent
and avoids duplicates.

$entity_view_mode_restriction['allowed_layouts'] = array_values($allowed_layouts);
$entity_display->setThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction', $entity_view_mode_restriction);
$updated_entity_displays[$entity_display->id()] = $entity_display->id();
}
}
// Replace layouts in layout builder sections.
$layout_builder_sections = $entity_display->getThirdPartySetting('layout_builder', 'sections');
if (!empty($layout_builder_sections)) {
foreach ($layout_builder_sections as $index => $section) {
$layout_name = $section->getLayoutId();
if (in_array($layout_name, $outdated_layouts)) {
$section_as_array = $section->toArray();
$section_as_array['layout_id'] = 'civictheme_three_columns';
$section_as_array['layout_settings']['label'] = 'CivicTheme Three Columns';
$section_as_array['layout_settings']['is_contained'] = ($layout_name === 'civictheme_one_column_contained');
// Move all components to 'main'.
foreach ($section_as_array['components'] as &$component) {
if ($component['region'] === 'content') {
$component['region'] = 'main';
}
}
$layout_builder_sections[$index] = Section::fromArray($section_as_array);
$updated_entity_displays[$entity_display->id()] = $entity_display->id();
}
}
$entity_display->setThirdPartySetting('layout_builder', 'sections', $layout_builder_sections);
}
if (in_array($entity_display->id(), $updated_entity_displays)) {
$entity_display->save();
$messages[] = (string) (new TranslatableMarkup('Updated @display_id display, replaced deprecated CivicTheme single-column layouts with civictheme_three_columns.', [
'@display_id' => $entity_display->id(),
]));
}
}
return implode("\n", $messages);
}