This issue has been kicking around for ages, but I don't think there's an overall issue for it yet.

In Drupal 7 we introduced #attached for CSS, JS and some other things so that assets which are needed by a specific element can be properly cached along with its parent element. If you use drupal_add_*() and your code doesn't run (because it's cached), then your CSS won't be added to the page.

The WSCCI initiative is try to allow for blocks to be rendered at their own URI, to allow for parallel rendering of page requests via mechanisms such as CSI/SSI/ESI, this runs into exactly the same problem (and some extra ones like the parent page not necessarily being served by PHP at all) as the render cache does.

So for either independently rendered blocks, or our render caching to work reliably, we need to finish the job and use this stuff consistently.

There's really two parts to this:

- supporting #title, #breadcrumbs and similar with #attached - extending the existing support for library/js/css/link, then ensuring core is consistent in using them and deprecating or removing wholesale the old functions.

- separately, figuring out how #attached will work with the response object - assuming most callbacks will be returning those rather than render arrays (although we'll likely need support for both while both co-exist anyway). I think there's already an issue for this bit but I can't find it at the moment.

Related issues:
#1811828: Use #attached to find the css/js of a view
#1830588: [META] remove drupal_set_title() and drupal_get_title()
#1029460: Find a better hook than hook_init() for drupal_add_js() and drupal_add_css()
#1839348: Make CSS/JS aggregation work for parallel rendering
#1762204: Introduce Assetic compatibility layer for core's internal handling of assets
#1947536: Convert drupal_get_breadcrumb() and drupal_set_breadcrumb() to a service
#1981644: Figure out how to deal with 'title/title callback'
#2032535: Resolve 'title' using the route and render array
#2073817: [META] Remove drupal_set_message()
#2061913: Remove drupal_set_breadcrumb and LegacyBreadcrumbBuilder in Views module
#2073819: [META] Remove direct calls to drupal_add_css()
#2073823: [META] Remove drupal_add_js() calls

Comments

sun’s picture

I followed up on #1830588-3: [META] remove drupal_set_title() and drupal_get_title() — it seems the same pattern is being proposed for some other things that share some aspects with the page title (e.g., breadcrumbs), and I don't think the proposed pattern will work out, so it might make sense to hash it out for the page title first.

nod_’s picture

A DX detail for getting rid of drupal_add_js is how we handle JS settings, using #attached is a pretty sucky DX compared to drupal_add_js().

First step of this was to change all drupal_add_js to drupal_add_library. The only part I couldn't change was a piece of code line 247 of the color.module.

And I guess I can close #1762204: Introduce Assetic compatibility layer for core's internal handling of assets and say it's B&L that'll fix it, right?

nod_’s picture

Issue summary: View changes

Updated issue summary.

catch’s picture

I couldn't find #1762204: Introduce Assetic compatibility layer for core's internal handling of assets when typing up the issue summary, added it. Not sure why we'd close that or what B&L means?

#attached may be sucky DX, we can obviously look at making it easier, or the response object extension will have to tackle that too.

nod_’s picture

Blocks and Layout.

Closing since both functions will (hopefully) go away with the new way of processing the #attach suff?

moshe weitzman’s picture

Blocks and Layouts ... Perhaps this is a slight DX regression but it is in the interest of caching that actually works. This isn't really optional.

catch’s picture

@nod_ I wouldn't close it yet, the new way of processing the #attach stuff doesn't exist yet, i know sdboyer is thinking about it but not sure where the issue is.

sdboyer’s picture

what, this can't be the issue? :)

unless someone recommends that i split it out, i plan to roll them in with the controller patch (#1812720: Implement the new panels-ish controller [it's a good purple]). that's code i'm literally writing that code right now, since we're down to ONE failure on blocks-as-plugins :)

David_Rothstein’s picture

Priority: Critical » Major

This is an important issue, but I don't believe it's a critical task (see http://drupal.org/node/45111 and http://drupal.org/node/1181250). Specifically, although we should fix this throughout core, if contrib modules are failing to use #attached it's already a bug against that module, not really core's fault directly. Also, none of the issues that this is a blocker for are marked critical - at least not as of a few minutes ago, anyway :)

If you disagree and feel this must still be marked critical, please provide a very specific justification and keep in mind that every critical issue significantly prevents other features that people are working hard on from getting into Drupal core, due to the issue queue threshold policy (until/unless #1810428: [Policy, no patch] Adjust major and critical task thresholds during code thaw is addressed, at any rate). Note that I am not singling out this issue for particular attention but rather (slowly) trying to go through the issue queue and do this in many places so that other features still have a chance to get into Drupal core soon without the thresholds getting in the way. I am trying my best to be impartial, and of course, just because an issue isn't marked "critical" certainly doesn't mean it's unimportant to work on. You can follow my progress via the tag I've just added to this issue :)

sdboyer’s picture

David_Rothstein’s picture

Issue tags: +major and critical issue threshold sweep

Sorry for the noise; I'm told that slashes in tags can break the autocomplete on drupal.org.

effulgentsia’s picture

Issue tags: +caching

I added a comment in #1762204-40: Introduce Assetic compatibility layer for core's internal handling of assets for why I think this issue is separate from that one, needs to happen regardless, and can be worked on in parallel. Adding the caching tag to try to keep everything that's needed for ESI discoverable.

xjm’s picture

Issue tags: -caching +D8 cacheability
xjm’s picture

Issue summary: View changes

Updated issue summary.

Crell’s picture

Issue tags: +WSCCI

Tagging.

tim.plunkett’s picture

manarth’s picture

So I picked off some of the low-hanging fruit in #2003524: Remove drupal_add_css from() help.module — use #attached, #2003564: Remove drupal_add_css() from block.module — use #attached, and #2003574: Remove drupal_add_css() from openid.module — use #attached.

The remaining drupal_add_css/drupal_add_js issues are split between:

  • Theme functions/preprocessors
  • Tests
  • Form-alters
  • Complex systems (e.g. using drupal_add_css() to get a list of the current css files, then doing some processing on that list

In other words, things that don't necessarily lend themselves to easily swapping to an #attached property on a render array.

manarth’s picture

Issue summary: View changes

Adding breadcrumbs issue

Wim Leers’s picture

#15: Thanks, reviewed & RTBC'd all three of them.

mcjim’s picture

Four calls to drupal_add_css in theme.maintenance.inc: issue and patch here: #2008270: Remove drupal_add_css() from template_preprocess_maintenance_page() — use #attached

mcjim’s picture

Issue summary: View changes

Updated issue summary.

mcjim’s picture

Issue summary: View changes

Added list of issues to remove drupal_add_css and drupal_add_js.

mcjim’s picture

mcjim’s picture

mcjim’s picture

Updated summary and added patch for book module: #2012526: Remove drupal_add_css() from book.module — use #attached

mcjim’s picture

catch’s picture

We don't have an issue for drupal_set_message() yet.

Crell’s picture

The right way to do drupal_set_message() is to use flash messages on the Symfony session, on the request object. Which of course is still blocked by Simpletest: #1858196: [meta] Leverage Symfony Session components

catch’s picture

That's not related to this issue at all.

Currently we put stuff in session, then at the end of the full page request, print the messages and empty the messages from session.

If something gets added to session after that happens, or the page doesn't print (i.e. a form redirect), then the messages will be in session for the next request and get printed.

Whether the messages are in the current session.inc session system of Symfony's has not bearing on this as far as I can tell - drupal_set_message() is more or less just a wrapper around $_SESSION, so the implementation is very similar, just with a sillier name.

Where it breaks down in *SI is that if you have a separate PHP request, the main one cannot know that something was added to $_SESSION in that request, so the messages will now get shown on the next full page.

At least in the Symfony code shipped with core, there's nothing at all which deals with this or any indication it's been dealt with. So there needs to be some layer which collects the messages at the end of the real requests, formats them, and makes them available to the overall page.

It's not as bad as assets/titles/breadcrumbs etc. because the messages would still get shown eventually, but it'd mean a lot more dpm()/error messages etc. being shown a request behind - as sometimes happens now if you try to use dpm() in the theme layer or similar.

effulgentsia’s picture

Priority: Major » Critical
Issue tags: -major and critical issue threshold sweep

I've discussed this with several people, including Crell, sdboyer, and Dries, and all agree on this being a critical issue, because of the huge performance impact that enabling block and other partial page element caches in core by default would have.

Specifically, although we should fix this throughout core, if contrib modules are failing to use #attached it's already a bug against that module, not really core's fault directly.

Right. This issue is primarily about fixing the usages in core. Though once we do that, removing the functions themselves, to force contrib to also do things correctly, is pretty easy.

catch’s picture

Wim Leers’s picture

Rerolled

However, there are still many other remaining calls to drupal_add_(css|js)(). We need issues/patches for those as well.

Wim Leers’s picture

Issue summary: View changes

Removing OpenID issue.

adammalone’s picture

Issue summary: View changes

Added related issue

mcjim’s picture

I had a list, somewhere. I'll update it and add the issues.
(Most are in tests, iirc.)

Wim Leers’s picture

#26: yep, typhonius is working on those already! :) I expect he'll file a new issue along with a patch.

dlu’s picture

Moved to cache system per #2050763-16: Refine "base system" component (notes on refactoring of "base system" category here: https://docs.google.com/a/acquia.com/spreadsheet/ccc?key=0AusehVccVSq2dF...).

webchick’s picture

Component: base system » cache system
Issue tags: +API change, +Approved API change

This will be an API change, but my understanding is this is implicitly approved because it makes render caching work.

xjm’s picture

Re-adding tags for #29. Tag Monster!

xjm’s picture

Issue summary: View changes

Added in related issue for bartik.theme

xjm’s picture

https://docs.google.com/a/acquia.com/document/d/1sgxD0l2OM91EqTekXryhdaX... has notes from Portland; looks like it somehow never got linked here.

We still need issues for drupal_set_breadcrumb() and drupal_set_message(). Adding the latter to the summary.

xjm’s picture

Issue summary: View changes

Updated issue summary.

catch’s picture

drupal_set_breadcrumb() has an issue open at #2061913: Remove drupal_set_breadcrumb and LegacyBreadcrumbBuilder in Views module.

drupal_set_messsage() is a funny one. Since that adds messages to $_SESSION it's not actually that bad if everything happens in a single request, messages aren't page assets like most other drupal_set_*() stuff.

However in a real *SI situation the message collection via checking $_SESSION isn't guaranteed to run. So that one doesn't feel like a release blocker but could use some discussion.

xjm’s picture

xjm’s picture

Issue summary: View changes

Updated issue summary.

xjm’s picture

Issue summary: View changes

Updated issue summary.

xjm’s picture

Split off #2073819: [META] Remove direct calls to drupal_add_css() and #2073823: [META] Remove drupal_add_js() calls to make it easier to get novice contributors working on those tasks.

catch’s picture

Status: Active » Closed (duplicate)

I think all the sub-issues are critical now, so we should close this one.

catch’s picture

Issue summary: View changes

Updated issue summary.

YesCT’s picture

Issue summary: View changes
Related issues: +#1743590: Isolated Block Rendering

#1743590: Isolated Block Rendering was postponed on this. since this is done, opening that one.