Populate data to drupalSettings to enable Dynamic Code Components

Created on 20 June 2025, 29 days ago

Overview

To enable dynamic usage of Code Components, we need to expose specific pieces of data to the Code components via `drupalSettings`. This includes information that is usually rendered by blocks like system branding or breadcrumb blocks, but will now be needed directly in Code components.

Proposed resolution

Populate the following values in `drupalSettings` under a new top-level key `xbData` (note: not under xb, which is reserved for internal Experience Builder needs):

- `drupalSettings.xbData.system_branding_block` with contents similar to:

  {
    "homeUrl": "/",
    "logo": "/core/misc/logo/drupal-logo.svg",
    "siteName": "My awesome website",
    "siteSlogan": "<p>This website is the best. It's better than all the rest.</p>"
  }

Other fields/data that needs to be populated are:
- Site logo
- Page title
- Site name
- Breadcrumb
- (data shape) system_branding_block.json
- (data shape) system_breadcrumb_block.json
- base_url

πŸ“Œ Task
Status

Active

Version

0.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain isholgueras

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @isholgueras
  • πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL

    I added more details and what I propose as the data shape.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Note: spend zero seconds on making pageTitle work, until πŸ› Handle adding page title to content Active is in. Hardcode some string for now.

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Added some implementation guidance.

  • πŸ‡³πŸ‡±Netherlands balintbrews Amsterdam, NL

    #3: We need the page title in drupalSettings.xbData regardless of that. πŸ› Handle adding page title to content Active will enable the page title block to work, but we would like to support building a dynamic code component where users can access the raw page title and add custom markup around it. It was amongst our original goals for ✨ Code Components as Block Overrides, step 1 Active , but we dropped that due to the challenges with page title block. Or maybe what you're saying is that we need πŸ› Handle adding page title to content Active even to be able to populate it in drupalSettings.xbData? πŸ™‚

  • Pipeline finished with Failed
    29 days ago
    Total: 846s
    #527394
  • Pipeline finished with Failed
    25 days ago
    Total: 1010s
    #530388
  • Pipeline finished with Failed
    25 days ago
    Total: 879s
    #530911
  • Pipeline finished with Success
    24 days ago
    Total: 778s
    #531358
  • πŸ‡ͺπŸ‡ΈSpain isholgueras

    I think it's ready for the first review round

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Nice work here!

    I'm missing three key things:

    1. JavaScriptComponent config entities should at minimum declare that they depend on drupalSettings
    2. JavaScriptComponent config entities should ideally declare that they depend on in drupalSettings β€” especially breadcrumbs data, because it massively worsens cacheability
    3. a strategy for avoiding worsened cacheability and scalability (see MR comments for details)

    I'm fine with a @todo + stable-blocking follow-up issue to address the dependency information, but shipping a 1.0 like this is a no-go.

  • πŸ‡ͺπŸ‡ΈSpain isholgueras

    There is only one thing left to do. How are the code component going to require that it depends on... breadcrumbs for example?

    I still missing how, if I'm creating a code component, I'm going to depend on breadcrumb or pageTitle.

    I mean, in the whole process as a user or XB user, I click in Add new, I get the editor and... where I define what features I will require?

    I was playing around with a new schema Choice property, drupalSettings, similar to a blockOverride and list there all the options allowing developers to add it to the yaml files, maybe in the future adding them to the UI, but that's very much out of scope here.

    Any thoughts here?

  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
    1. πŸ‘ The current MR is based on the simple on/off presence of preg_match('/drupalSettings\.xbData/', $component->getJs(). I agree with the pragmatism until such a time we can use explicit import statements.
    2. πŸ‘Ž While better than when I reviewed it in #8 late last week, it still has too much of a negative impact on cacheability and hence scalability. See my analysis and rough proposal, and my implementation outline. That outline partially works. Next step: debug why \Drupal\experience_builder\Hook\ComponentSourceHooks::jsSettingsAlter() is not being called.
    3. πŸ‘Ž The problem with the absence of explicit import statements for specific subsets of data is that this de facto becomes an API we can not ever evolve nor change. So, I think it should be tweaked slightly, similar to what we did with XB's internal HTTP API at πŸ“Œ Define API URI naming conventions Active : we should version it.

      So instead of:

      drupalSettings?.xbData?.branding?.homeUrl
      

      it should become
      drupalSettings?.xbData?.v0?.branding?.homeUrl

    … which will allow for evolvability, backwards compatibility layers, deprecation warnings etc.

  • πŸ› This currently contains a race condition, because it inspects ::getJs(), which is the saved JS, not the auto-save JS. This needs to reflect either. In fact, it probably makes sense to ALWAYS load all drupalSettings.xbData for draft components, just to avoid race conditions: if I'd add drupalSettings while editing the code in the code editor, they wouldn't be populated yet? See πŸ› JavaScriptComponent CSS libraries should depend on AssetLibrary libraries Active for inspiration.
  • Pipeline finished with Failed
    19 days ago
    Total: 663s
    #535233
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Oh, a crucial bit I forgot:

    In Drupal 11, how does the cacheability for `drupalSettings` get associated with the response?

    β€” I

    • Settings added in system_js_settings_build() are cached and reused
    • Settings added in system_js_settings_alter() are regenerated on every request

    β€” Claude

    This is why in the commit I pushed, I used hook_js_settings_alter(). XB doesn't create any new problem/challenge then, but just piggybacks on what Drupal core does πŸ‘

  • Pipeline finished with Failed
    19 days ago
    Total: 610s
    #535388
  • Pipeline finished with Failed
    19 days ago
    Total: 1485s
    #535394
  • Pipeline finished with Failed
    19 days ago
    Total: 713s
    #535922
  • Pipeline finished with Failed
    19 days ago
    Total: 1129s
    #535943
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Paired with @isholgueras to get #12.2 working. Got it working, but not in the way I liked. So I dug deeper. Turns out we're running into a core bug: πŸ› `AssetResolver::getJsSettingsAssets()` calls `::getLibrariesToLoad()` with wrong asset type Active . Worked around it in https://git.drupalcode.org/project/experience_builder/-/merge_requests/1... πŸ‘

    Over to @isholgueras to address the remaining feedback β€” we're on the same page πŸ₯³

  • Pipeline finished with Failed
    18 days ago
    Total: 678s
    #536252
  • Pipeline finished with Failed
    18 days ago
    Total: 672s
    #536258
  • Pipeline finished with Failed
    18 days ago
    Total: 1060s
    #536263
  • Pipeline finished with Canceled
    18 days ago
    Total: 679s
    #536303
  • Pipeline finished with Success
    18 days ago
    Total: 741s
    #536323
  • Pipeline finished with Canceled
    18 days ago
    Total: 681s
    #536339
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Much better! 🀩

  • Pipeline finished with Failed
    18 days ago
    Total: 1257s
    #536344
  • Pipeline finished with Skipped
    18 days ago
    #536361
  • Pipeline finished with Skipped
    18 days ago
    #536362
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί
  • πŸ‡ͺπŸ‡ΈSpain isholgueras

    After chatting with @balintbrews, the resolution for this issue is not correct.

    drupalSettings.xbData should be in global scope and appear only once. This branch does too much and it has some errors:
    - The preview (top right) doesn't work. When users create code, the client is not yet talking to the backend.
    - The non-compiled code wont work.

    The front-end API will consume this settings with methods (not decided yet) like getPageData() (for title and breadcrumb) and getSiteData() (for base URL and branding).

  • πŸ‡ΊπŸ‡ΈUnited States effulgentsia

    From chatting with @balintbrews, as I understand it there's only two problems with what got merged here:

    1. πŸ“Œ Improve the way to detect if the code component has drupalSettings Active (that issue had already been opened previously, I just now re-titled it).
    2. πŸ› xbData.* libraries aren't reliably attached for use by the preview while editing a code component Active (I just now filed that issue).

    Assuming it's only those two problems, let's handle them there, and I'm moving this one back to Fixed.

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

  • Production build 0.71.5 2024