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:

    - 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?

  • 1
    It does look like extending is for themes only, but even so - it seems a shame to override and maintain your own version of the whole library for this (if a new method gets added in the core version you'll have to add it in your own version and so on). I'd be tempted to just monkey patch that one function - declare your own library with a JS file containing (a more complete version of) Drupal.theme.mediaEmbedEditButton = function() { return 'html'; }. Make sure your library has a dependency on the core library, so its JS is included first, and the browser should do the rest
    – Clive
    May 8 '20 at 19:18
  • My hope was indeed to have to maintain just a JS file basically containing little more than Drupal.theme.mediaEmbedEditButton = function() { return 'custom button html'; } (plus the necessary library-extend and custom library stuff to have it picked up). I definitely don't want to override and maintain a whole core library. Sorry if that wasn't clear from my long-winded text, haha. The small JS worked when I made a custom admin theme (other unrelated issues notwithstanding). Just thought it was odd to only be supported for themes, not modules. Maybe I should file a feature request. May 8 '20 at 20:11
  • @MaxStarkenburg this is a great question, well-researched and documented. I think the answer is clear, though: you will need to create a custom admin theme to avoid the chore of overriding and maintaining a whole core library. Sep 25 at 5:53

It's not possible, read this: https://www.oreilly.com/library/view/drupal-8-development/9781788290401/080b0131-1b64-4900-8535-55c82dba4200.xhtml

Manipulating libraries from hooks

Modules have the ability to provide dynamic library definitions and alter libraries. A module can use the hook_library_info() hook to provide a library definition. This is not the recommended way to define a library, but it is provided for edge use cases.

Modules do not have the ability to use libraries-override or libraries-extend, and need to rely on the hook_library_info_alter() hook. You can check out this hook in core/lib/Drupal/Core/Render/theme.api.php or at https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_library_info_alter/8.

source: Drupal 8 Development Cookbook - Second Edition by Matt Glaman

  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 16 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.