Is it possible to use libraries-extend from a module (instead of theme)?

Problem/Motivation

I’m interested in altering/overriding the little bit of HTML markup that’s output by core, in my case Drupal.theme.mediaEmbedEditButton (from core/modules/media/js/media_embed_ckeditor.theme.js), but without hacking core, of course. It seems I could do this in a custom theme by adding:

libraries-extend:
  media/media_embed_ckeditor_theme:
    - my_theme/my_library

(plus the info for my_library in my_theme.libraries.yml, and the JS file in my theme with the function that would return the new markup).

However, since this shows up in places like /node/add/page that only uses the admin theme (we’re using Seven), such additions to my_theme wouldn’t apply. While it seems like I could make a bare-bones my_admin_theme, pulling in Seven as base, I’d like to keep this override near other related customizations in a custom module (I also ran into other cans of worms when I tried this as a proof of concept).

What (else) I’ve tried

I tried putting in libraries-extend: from above in my_module.info.yml (plus the addition to my_module.libraries.yml and the new JS, of course), but this didn’t seem to work (kinda not surprising, since the docs I’m finding only seem to reference libraries-extend in the same breath as themes). I also tried using hook_field_widget_form_alter() in my_module.module to attach my override library via $element['#attached']['library'][] but didn’t have luck there either.

Am I stuck with with making a custom admin theme if I want to use libraries-extend? (I’m guessing this is also a problem for libraries-override.) Or am I missing some hook or something I could instead add to my custom module to accomplish overriding the output made by Drupal.theme.mediaEmbedEditButton?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Is it possible to use libraries-extend from a module (instead of theme)?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.