Problem/Motivation

In #3487971: Convert system_page_attachments to OOP we're moving system_page_attachments() into BareHtmlPageRenderer wholesale, then calling it from system's hook_page_attachments() implementation.

At least some, possibly most of the logic is not actually needed by BareHtmlPageRenderer - we should try to move what we can back to system's page_attachments hook implementation.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3488965

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

catch created an issue. See original summary.

mstrelan’s picture

Added the related issue where the call to system_page_attachments was first added to BareHtmlPageRenderer

mstrelan’s picture

I've had a look at everything this function does and broken it down below. From what I can tell most of it is still required, except maybe "Attach libraries used by this theme" and "Handle setting the "active" class on links". Would be good to get a second opinion.

// Ensure the same CSS is loaded in
// \Drupal\Core\Theme\ThemePreprocess::preprocessMaintenancePage().
$page['#attached']['library'][] = 'system/base';
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
  $page['#attached']['library'][] = 'system/admin';
}

I'm not sure if this is attaching it for maintenance pages, or for the installer to match maintenance pages. If it's the former, well ThemePreprocess::preprocessMaintenancePage already adds these, so I'll assume it's the latter. And Claro already depends on system/admin so we get these attached on the installer already. But I guess the installer could use a different theme that doesn't have that dependency, so this would be required.

// Attach libraries used by this theme.
$active_theme = \Drupal::theme()->getActiveTheme();
foreach ($active_theme->getLibraries() as $library) {
  $page['#attached']['library'][] = $library;
}

Without this I still get theme libraries attached on the installer, e.g. the first one that xdebug sees is core/drupal.message and I see /core/themes/claro/css/components/messages.css in the page markup. But maintenance pages have no styles without this.

// Attach favicon.
if (theme_get_setting('features.favicon')) {
  $favicon = theme_get_setting('favicon.url');
  $type = theme_get_setting('favicon.mimetype');
  $page['#attached']['html_head_link'][][] = [
    'rel' => 'icon',
    'href' => UrlHelper::stripDangerousProtocols($favicon),
    'type' => $type,
  ];
}

Without this there is no favicon on the installer.

// Attach default meta tags.
// ...
foreach ($meta_default as $key => $value) {
  $page['#attached']['html_head'][] = [$value, $key];
}

As above.

// Handle setting the "active" class on links by:
// - loading the active-link library if the current user is authenticated;
// - applying a response filter if the current user is anonymous.
// @see \Drupal\Core\Link
// @see \Drupal\Core\Utility\LinkGenerator::generate()
// @see \Drupal\Core\Theme\ThemePreprocess::preprocessLinks()
// @see \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
$page['#cache']['contexts'][] = 'user.roles:authenticated';
if (\Drupal::currentUser()->isAuthenticated()) {
  $page['#attached']['library'][] = 'core/drupal.active-link';
}

Unless I'm overlooking something, by definition it's impossible for the user to be authenticated at install time.

mstrelan’s picture

Status: Active » Needs review
Related issues: +#3428339: Remove calls to system_page_attachments()

Possibly a duplicate of #3428339: Remove calls to system_page_attachments() actually. Although maybe that can be repurposed to just moving the library from system to core.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Can the summary be flushed out some please, sorry to be that guy.

mstrelan’s picture

I think we need @catch to weigh in here. I think it might be better to focus on the work in #3428339: Remove calls to system_page_attachments() and see if there's anything left to do here.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.