Overview

We made sure to generate a experience_builder/asset_library.<name>.draft asset library in #3500386: Code Components should render with their auto-saved state (if any) when rendered in the XB UI … but we never actually load it! 😅

IOW: we forgot to update experience_builder_page_attachments().

Proposed resolution

User interface changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

wim leers created an issue. See original summary.

wim leers’s picture

Status: Active » Needs work
diff --git a/experience_builder.module b/experience_builder.module
index 541a0f6c1..2d28db464 100644
--- a/experience_builder.module
+++ b/experience_builder.module
@@ -13,6 +13,7 @@ ini_set('assert.active', 1);
 use Drupal\Component\Serialization\Json;
 use Drupal\Core\Block\BlockManagerInterface;
 use Drupal\Core\Block\BlockPluginInterface;
+use \Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
 use Drupal\Core\Block\MainContentBlockPluginInterface;
 use Drupal\Core\Entity\EntityPublishedInterface;
@@ -26,6 +27,7 @@ use Drupal\Core\Plugin\Discovery\YamlDiscovery;
 use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Drupal\Core\Url;
 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
+use Drupal\experience_builder\AutoSave\AutoSaveManager;
 use Drupal\experience_builder\Entity\AssetLibrary;
 use Drupal\experience_builder\Entity\Component;
 use Drupal\experience_builder\Entity\JavaScriptComponent;
@@ -784,7 +786,37 @@ function experience_builder_dialog_library_customize(string $admin_theme_name):
  * Implements hook_page_attachments().
  */
 function experience_builder_page_attachments(array &$page): void {
-  $page['#attached']['library'][] = 'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID;
+  // Load the auto-save/draft version of the global asset library in the XB UI,
+  // if it exists and is accessible for the current user.
+  if (str_starts_with(\Drupal::routeMatch()->getRouteName(), 'experience_builder.api.layout.')) {
+    $global_asset_library = AssetLibrary::load(AssetLibrary::GLOBAL_ID);
+
+    $auto_saved_global_asset_library = NULL;
+    $auto_save_manager = \Drupal::service(AutoSaveManager::class);
+    assert($auto_save_manager instanceof AutoSaveManager);
+    $auto_save = $auto_save_manager->getAutoSaveData($global_asset_library);
+    if (!$auto_save->isEmpty()) {
+      \assert($auto_save->data !== NULL);
+      $auto_saved_global_asset_library = $global_asset_library->forAutoSavePreview($auto_save->data);
+    }
+    $page['#cache']['tags'][] = AutoSaveManager::CACHE_TAG;
+
+    // If the current user is allowed to access draft versions of the global asset
+    // library and there is currently a draft, load the draft.
+    // @todo Update in https://www.drupal.org/project/experience_builder/issues/3508694
+    $access = $global_asset_library->access('view', return_as_object: TRUE);
+    CacheableMetadata::createFromObject($access)->applyTo($page);
+    if ($auto_saved_global_asset_library !== NULL && $access->isAllowed()) {
+      $page['#attached']['library'][] = 'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID . '.draft';
+    }
+    else {
+      $page['#attached']['library'][] = 'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID;
+    }
+  }
+  // Outside the XB UI, never load the draft.
+  else {
+    $page['#attached']['library'][] = 'experience_builder/asset_library.' . AssetLibrary::GLOBAL_ID;
+  }
 
   // Adds `track_navigation` library to all pages, to allow XB's "Back" link to know which URL to go back to.
   $page['#attached']['library'][] = 'experience_builder/track_navigation';
wim leers’s picture

balintbrews’s picture

This just became very relevant for a good experience after #3516390: Compile Tailwind CSS globally for code components lands.

wim leers’s picture

Issue tags: +Needs tests

Sample fix is in #2, tests still needed.

tedbow made their first commit to this issue’s fork.

tedbow’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +sprint
wim leers’s picture

Assigned: Unassigned » tedbow
Status: Needs review » Needs work
tedbow’s picture

Assigned: tedbow » wim leers
Status: Needs work » Needs review

2 e2e test fails, passed locally., passed on re-run. PHPUnit tests passed

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs review » Reviewed & tested by the community

RTBC'd with 2 caveats/requests: 1, 2.

If not addressed here, we need a follow-up for especially the latter.

tedbow’s picture

re #12 I created #3523460: Create loadAutoSave() helper function as follow-up for 2) and refactored for 1)

  • tedbow committed e0ae76fb on 0.x
    Issue #3508937 by tedbow, wim leers: Global AssetLibrary should render...
tedbow’s picture

Status: Reviewed & tested by the community » Fixed

🎉

wim leers’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

effulgentsia’s picture

Issue tags: -sprint