- Issue created by @effulgentsia
- 🇺🇸United States effulgentsia
I tagged this as beta blocking because 🌱 Plan how to evolve code component overrides Active is, and this is part of that.
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
FYI: this is possible since 📌 Populate data to drupalSettings to enable Dynamic Code Components Active and 🐛 xbData.* libraries aren't reliably attached for use by the preview while editing a code component Active .
This will need to update
\Drupal\experience_builder\CodeComponentDataProvider::getRequiredXbDataLibraries()
to stop checking for_drupalSettings_xbData_v0_breadcrumbs
and_drupalSettings_xbData_v0_branding
, because it'll need to check for the presence of a call to::getSiteData()
and/or::getPageData()
instead.This was agreed in 📌 Improve the way to detect if the code component has drupalSettings Active as an intermediate step, see #3533458-5: Change CodeComponentDataProvider::getRequiredXbDataLibraries() to base its logic on information provided by the front-end rather than on naive string/regex matching → for the proposed plan, +1'd by @balintbrews at #7 and by @larowlan at #9.
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
FYI: this is possible since 📌 Populate data to drupalSettings to enable Dynamic Code Components Active and 🐛 xbData.* libraries aren't reliably attached for use by the preview while editing a code component Active .
This will need to update
\Drupal\experience_builder\CodeComponentDataProvider::getRequiredXbDataLibraries()
to stop checking for_drupalSettings_xbData_v0_baseUrl
and_drupalSettings_xbData_v0_pageTitle
, because it'll need to check for the presence of a call to::getSiteData()
and/or::getPageData()
instead.This was agreed in 📌 Improve the way to detect if the code component has drupalSettings Active as an intermediate step, see #3533458-5: Change CodeComponentDataProvider::getRequiredXbDataLibraries() to base its logic on information provided by the front-end rather than on naive string/regex matching → for the proposed plan, +1'd by @balintbrews at #7 and by @larowlan at #9.
- 🇳🇱Netherlands balintbrews Amsterdam, NL
I expanded the issue to cover page title as well. Also adding credits for discussions prior to this issue being created.
- First commit to issue fork.
- Merge request !1252Issue #3534617: Add breadcrumb, page title, branding → (Merged) created by larowlan
- 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
Pushed a test with each of these three components following lead in 📌 Build a navigation code component Active (and on top of that).
Changes for this branch only are in this commit
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
@balintbrews merged 📌 Build a navigation code component Active 🥳, the MR here now needs rebasing on top of that.
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
Also, #3 has not yet been addressed. Without that, this cannot work reliably.
I recommend starting with updating
tests/fixtures/recipes/test_site/config/experience_builder.component.js.xb_test_code_components_using_drupalsettings.yml
, which should then cause the tests to fail, which will force updatinggetRequiredXbDataLibraries()
. - 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
Don't make the components use drupalSettings.xbData... directly. Instead, create API functions getPageData() (for breadcrumb and page title) and getSiteData (for site name, logo, home URL, site slogan, and base URL) within @/lib/utils or similar.
Ah sorry, I was following the lead of the navigation one
- 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10
Going to assume everyone else is asleep and try and move the needle on this
- 🇫🇮Finland lauriii Finland
If we are not using
useSWR
, the XB date fetch feature doesn't work with this. Not sure what to do about that – it's not great that some of our utilities are using it and others are not.For some reason, this wasn't working outside of XB. It looks like the values are
null
indrupalSettings
. Has anyone else verified this works outside of XB? - 🇳🇱Netherlands balintbrews Amsterdam, NL
#18:
If we are not using useSWR, the XB date fetch feature doesn't work with this. Not sure what to do about that – it's not great that some of our utilities are using it and others are not.
That's a really good point. Somehow I've been thinking that we'd use
useSWR
only for HTTP requests, which is its primary purpose, but probably nothing stops us from using it for reaching intodrupalSettings
. Maybe if the new API methods returned promises, it would be all we need? - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
Debugged, and @larowlan's updated
::getRequiredXbDataLibraries()
logic works fine. (But we should really still do #3533458-12: [PP-1] Change CodeComponentDataProvider::getRequiredXbDataLibraries() to base its logic on information provided by the front-end rather than on naive string/regex matching → — not here/now though.)Root cause
Found the culprit, and it's 2-fold, both missed bugs from 📌 Populate data to drupalSettings to enable Dynamic Code Components Active :
- We use
hook_js_settings_build()
(whose results are cached), but should have usedhook_js_settings_alter()
(whose results are not cached). A simple mistake. Not worth adding test coverage for IMHO. - The
\Drupal\experience_builder\CodeComponentDataProvider::getPartialXbDataFromSettingsV0()
logic was usingisset()
… which has very weird/special behavior when it comes to key-value pairs where the value isNULL
… 😅 Switching toarray_key_exists()
is all we need. PHP--
Once again: never use
isset()
, and always do exact assertions of the full data structure expected. I was less strict while reviewing that one in order to keep things moving and it looked simple/innocent enough, and look, it bit us. 😬Solution
- Expand test coverage: https://git.drupalcode.org/project/experience_builder/-/merge_requests/1... → failed 👍
- Fix: https://git.drupalcode.org/project/experience_builder/-/merge_requests/1...
Final sign-off from @balintbrews now, because the back-end pieces are ready (and approved) 👍
- We use
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
Spoke too soon; there's a regression, probably because Drupal's asset library system calls
hook_js_settings_build()
andhook_js_settings_alter()
at different times 😬 - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
Green again for @balintbrews 👍 This is a bit of kinda tricky logic, but it's worth it: the asset library definnitions in
*.libraries.yml
show an outline of what to expect (matching best practices in Drupal core), it supports overrides, and it loads only what's necessary (which is currently all tiny but may not remain the case) hence paving the path for #3533458-12: [PP-1] Change CodeComponentDataProvider::getRequiredXbDataLibraries() to base its logic on information provided by the front-end rather than on naive string/regex matching → .The last bit I discovered is that 📌 Populate data to drupalSettings to enable Dynamic Code Components Active 's interpretation of
baseUrl
is an absolute URL, without the subdirectory. That's not howdrupalSettings.path.baseUrl
works … so I switched to that. Then I realized it was literally in the instructions/issue summary of 📌 Populate data to drupalSettings to enable Dynamic Code Components Active … so reverted it once again. So documented that confusing difference explicitly:// ⚠️ Not the same as `drupalSettings.path.baseUrl` nor Symfony' // definition. // @see \Symfony\Component\HttpFoundation\Request::getBaseUrl() // @see \Drupal\system\Hook\SystemHooks::jsSettingsAlter()
- 🇳🇱Netherlands balintbrews Amsterdam, NL
@lauriii, I put more thought into what you said about not using
useSWR
. While standardizing on that sounded sensible at first, I don't think it's the way to go. The main reason of providinguseSWR
is to ensure reactivity in the components while async data fetching is happening. That's not the case here, the data is synchronously available in thewindow
object, we just need to return it. To make our utility functions work withuseSWR
we'd need to turn them into async functions with no good reason. Not to mention that we wouldn't make use of SWR's caching. I understand that the code editor's data explorer is tied touseSWR
, but that data explorer was built to explore more complex data shapes and potentially larger amount of data, whereas the new API methods can be easily documented due their very simple data shapes. In case we miss the data explorer functionality, I propose we make that happen later as part of the API methods, as opposed to forcing component authors to wrap them in a React hook.Assigning back to Wim to wrap up how
baseUrl
is populated. - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
That's not the case here, the data is synchronously available
💯
This would only not be true on sites that do not do "regular" page loads, i.e. sites using something like http://drupal.org/project/refreshless.
- 🇬🇧United Kingdom justafish London, UK
-
balintbrews →
committed 9a02f969 on 0.x authored by
larowlan →
Issue #3534617 by wim leers, larowlan, justafish, balintbrews, lauriii,...
-
balintbrews →
committed 9a02f969 on 0.x authored by
larowlan →
Automatically closed - issue fixed for 2 weeks with no activity.