Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Wim Leers on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0-BETA5
Issue links:
Description:
Removed already-deprecated APIs, the removal of which we've been working towards for several years, hence strictly speaking these are not API changes:
_drupal_add_css()
_drupal_add_js()
_drupal_add_library()
Actual API changes (most changes affect <1% of developers, changes applying to at least 5% of developers are in bold):
- Removed:
drupal_sort_css_js()
— this was only used internally, and because it was legacy procedural code, strictly speaking, anybody could be calling this. In practice, this is extremely unlikely - Removed:
drupal_get_css()
— less than 1% of contrib modules use this, if even 0.5% — it's only necessary if they want to output the HTML to load CSS manually for a very special use case - Removed:
drupal_get_js()
— same as above, but for JS - Changed:
drupal_process_attached()
— instead of processing/storing the attached assets in a global (which is precisely what we've been working to get rid of for several years), this now only handles non-asset attachments. - Added:
AttachedAssets(Interface)
: a value object for tracking the attached assets for a response, as well as the libraries the client has already loaded, if any. This is a value object thatAssetResolver
's methods accept, and it's howAssetResolver
can remain stateless. - Added:
AssetResolver(Interface)
: given anAttachedAssets
value object, this stateless service can determine which concrete CSS and JS assets should be loaded. Instead of myriads of strange edge cases being encapsulated in_drupal_add_(css|js)()
anddrupal_get_(css|js)()
, this now makes it abundantly clear what is happening. But, not to worry, 99% of developers won't ever to have interact with it; Drupal automatically calls it with the attached assets when rendering responses.
The methodsAssetResolver::getCssAssets()
andAssetResolver::getJsAssets()
now contain the logic of_drupal_add_css()
+drupal_get_css()
and_drupal_add_js()
+drupal_get_js()
, respectively.
This new class/service reuses the test coverage inAttachedAssetsTest
. - Added:
LibraryDependencyResolver(Interface)
: given a list of libraries, can expand that list of libraries with their dependencies, but can also calculate the minimal representative subset. This logic used to be implemented in a very convoluted way in_drupal_add_library()
, relying on statics to work. - Added:
AjaxResponse::setAttachments()
, to no longer rely on the statics in_drupal_add_(css|js)()
to make Ajax responses aware of CSS/JS assets attached in render arrays. - Removed:
\Drupal\Core\Render\Element\(Scripts|Styles)
: these were used bydrupal_get_(css|js)()
, respectively, for rendering the CSS/JS asset collection into HTML. They're now obsolete. Their documentation has been retained; it's been moved to(Css|Js)CollectionRenderer
. - Changed:
drupalSettings.ajaxPageState
is only added if it's actually necessary, i.e. if thecore/drupal.ajax
library is loaded. - Added:
hook_(css|js|js_settings)_alter()
receive an additional parameter now:\Drupal\Core\Asset\AttachedAssetsInterface $assets
. This provides extra context, and might be necessary in the decision in what to do in those alter hooks.system_js_settings_alter()
uses it to generate theajaxPageState
setting.
Impacts:
Module developers