Generic way to attach library to layout_builder?

Created on 18 July 2022, over 2 years ago
Updated 21 April 2024, 8 months ago

Problem/Motivation

Currently the libraries are attached to layout builder using hook_page_attachments() combined with gin_lb_is_layout_builder_route() that uses preg_match to determine if the current page / path is layout builder.

/**
 * Implements hook_page_attachments().
 */
function gin_lb_page_attachments(array &$attachments) {
  if (gin_lb_is_layout_builder_route()) {
    $attachments['#attached']['library'][] = 'gin_lb/gin_lb_init';
    $attachments['#attached']['library'][] = 'gin/gin_ckeditor';
    $attachments['#attached']['library'][] = 'claro/claro.jquery.ui';
    $attachments['#attached']['library'][] = 'gin_lb/gin_lb_form';

    $config = \Drupal::config('gin_lb.settings');
    if ($config->get('toastify_cdn')) {
      $attachments['#attached']['library'][] = 'gin_lb/toastify';
    }
  }
}

/**
 * Returns true if the current route is a layout builder route.
 *
 * @return bool
 *   Returns true for layout builder routes.
 */
function gin_lb_is_layout_builder_route() {
  $gin_lb_is_layout_builder_route = drupal_static(__FUNCTION__);
  if ($gin_lb_is_layout_builder_route !== NULL) {
    return $gin_lb_is_layout_builder_route;
  }
  $route_name = \Drupal::routeMatch()->getRouteName();
  $gin_lb_is_layout_builder_route = FALSE;
  if ($route_name !== NULL && preg_match('/^(layout_builder\.([^.]+\.)?)/', $route_name)) {
    $gin_lb_is_layout_builder_route = TRUE;
  }
  \Drupal::moduleHandler()->alter('gin_lb_is_layout_builder_route', $gin_lb_is_layout_builder_route, $context);
  return $gin_lb_is_layout_builder_route;
}

That works, but looks like a workaround and I'm wondering if Core (layout_builder) shouldn't provide a "cleaner" way to determine we're in layout builder and allow to attach libraries?

We're having the same requirement in #3218238: Find a better way to attach the drowl_admin/layout_builder library to layout builder β†’ but gin_lb is used much wider, so I thought it would make sense to open the issue here. If someone knows a core issue for this, please add a link.

Thanks!

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component
Layout builderΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Moving this to core as of #2

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Reviewing the comments in this thread and having a look at some contrib, it looks like there is already a way to do this.

    Comments from larowlan in #6 are correct in that you can just use hook_element_info_alter()

    larowlan
    But if you just want to attach them to layout builder you should use the hook_element_info_alter hook and add an extra process callback for the layout builder render element and attach it in that

    larowlan
    IE there already a hook

    Example of this in Contrib

    function epa_layouts_element_info_alter(array &$types) {
      if (isset($types['layout_builder'])) {
        $types['layout_builder']['#attached']['library'][] = 'epa_layouts/epa_layouts';
      }
    }
    

    I think this can be closed?

  • Status changed to Closed: works as designed 8 months ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    No further updates in 3 months, examples have been provided on how to do this. I'm now marking this issue as closed.

    Thanks!

Production build 0.71.5 2024