I would like to use DataTables.js for tables created by content editors in the main body field of a content type. I already have the library installed in my libraries.yml.
I had an idea that I could avoid calling the library globally, but instead have it only be called on a page that has an HTML <table>
element rendered.
That idea came from drupal.org documentation here, which offers up this code example for the hook_element_info_alter()
function:
function fluffy_element_info_alter(array &$types) {
if (isset($types['table'])) {
$types['table']['#attached']['library'][] = 'fluffiness/fancy_table';
}
}
However when I checked into the theme.api, I saw this code is intended for people making modules and indeed the documentation section I saw it in was for modules.
Can I use this same or similar bit of code in my theme_name.theme file to apply to all nodes? Is there something else I should do instead?
Thanks for any advice you can offer.