Problem/Motivation

After upgrading to 6.2.x for Drupal 10, our offcanvas element edit forms were completely broken. We tracked this down to webform_css_alter not removing core's offcanvas CSS. This was happening because, for whatever reason, \Drupal::service('router.admin_context')->isAdminRoute(); was returning FALSE.

The weird thing is, if I run drush ev "\Drupal::service('library.discovery')->clearCachedDefinitions();" and reload the page it works fine. But as soon as I do a full cache clear after that it breaks again.

This commit removed the explicit Claro check too.

Steps to reproduce

Unfortunately I'm not sure how to reproduce this on a vanilla setup, will keep digging though.

Proposed resolution

Can we use \Drupal\webform\WebformRequest::isWebformAdminRoute instead?

Should we also move the _admin_route route option from the route subscriber into the routing.yml definitions?

Comments

acbramley created an issue. See original summary.

acbramley’s picture

Also worth noting that https://www.drupal.org/node/2888767 states:

In general, hook_css_alter() and hook_js_alter() may now be called from a separate HTTP request from the one rendering the HTML (this was already the case with some contributed modules), and as such cannot vary on all information from the request such as route.

So we may need to change this anyway if routes aren't reliable in this context.

fenstrat’s picture

Status: Active » Needs review
StatusFileSize
new1.05 KB

Here's the WebformRequest::isWebformAdminRoute() approach. It fixes the issue in our case. But it's not convincing as the correct fix™ because isWebformAdminRoute()is still accessing the admin context.

fenstrat’s picture

So in our case the admin context check in isWebformAdminRoute() is picking up the admin route (which oddly goes against what #2 is suggesting - that was our issue in the first place, \Drupal::service('router.admin_context')->isAdminRoute() === FALSE). Though @acbramley noted he did see 2 requests when debugging through getCurrentRequest().
So then because it is being seen as an admin route that means it then falls through to the (preg_match('/^(webform\.|^entity\.([^.]+\.)?webform)/', $route_name)) ? TRUE : FALSE; check which picks correctly picks up the admin route.

As that last check is done on route_name, and that seems to be more reliable in hook_css_alter, then this seems fine.

Also, reusing \Drupal::service('webform.request')->isWebformAdminRoute() and deferring to existing code seems like a sane approach here anyway.

acbramley’s picture

#3 works (for us) because by the time webform_css_alter is called, $this->isAdminRoute is already set on the WebformRequest service and returns early.

Debugging into isWebformAdminRoute, the first time it's called (from webform_theme_suggestions_form_element that calls WebformElementHelper::isWebformElement.

For whatever reason, this initial code path does return TRUE for $this->adminContext->isAdminRoute()

jrockowitz’s picture

The problem and solution make sense.

What are the steps required to replicate this issue?

  • fenstrat authored 43034a14 on 6.2.x
    Issue #3347104 by fenstrat: webform_css_alter sometimes doesn't remove...
jrockowitz’s picture

Status: Needs review » Fixed

Using WebformRequest::isWebformAdminRoute will remove the offcanvas

catch’s picture

This came up in slack https://drupal.slack.com/archives/C78MFLN9K/p1685066450956139 persumably without realising this issue was open.

Could webform look for its own CSS amongst the CSS in hook_css_alter(), and if it's there, remove the offcanvas resets?

lauriii’s picture

I don't think this actually works with 10.2.x because \Drupal\webform\WebformRequest::isWebformAdminRoute also depends on the current request. You'll probably have to implement something like what's proposed in #9. More info can be found in this CR https://www.drupal.org/node/2888767.

  • eb179a84 committed on 6.2.x
    Revert "Issue #3347104 by fenstrat: webform_css_alter sometimes doesn't...
jrockowitz’s picture

Title: webform_css_alter sometimes doesn't remove offcanvas files » [Drupal 10.x] webform_css_alter sometimes doesn't remove offcanvas files
Status: Fixed » Needs work

I am going to roll back the commit since I think a better approach might be detecting if webform-specific CSS is present.

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new1.7 KB

Looks like #9 is much better and safer approache.

catch’s picture

#13 looks as simple as I'd hoped it would be!

jrockowitz’s picture

luke.leber’s picture

I've manually tested against 9.5.x-dev and 10.1.x-dev.

9.5.x-dev works great -- even without the patch (as expected).
10.1.x-dev works great -- even with CSS aggregation enabled. #13 seems to fix the "files can be aggregated in a separate process" problem.

+1 RTBC from me!

catch’s picture

Status: Needs review » Reviewed & tested by the community

Moving #13 to RTBC per the manual testing in #16.

jrockowitz’s picture

I am going to back port this to 6.1.x because I am continually merge 6.1.x into 6.2.x

jrockowitz’s picture

StatusFileSize
new348 bytes
jrockowitz’s picture

StatusFileSize
new1.79 KB
luke.leber’s picture

Pedantically, I gave #20 a test run against 9.5.x and everything seemed okay -- as in it worked great before, and worked equally as great after!

  • jrockowitz authored 643cba86 on 6.1.x
    Issue #3347104 by jrockowitz, fenstrat: [Drupal 10.x] webform_css_alter...

  • jrockowitz authored 643cba86 on 6.x
    Issue #3347104 by jrockowitz, fenstrat: [Drupal 10.x] webform_css_alter...

  • jrockowitz authored 643cba86 on 6.2.x
    Issue #3347104 by jrockowitz, fenstrat: [Drupal 10.x] webform_css_alter...
jrockowitz’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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