Hello,

I'm new in drupal 8 world and I'm trying to make my first steps in theming.

Because I want to use different libraries to different pages I realized that I cannot attach libraries If I use hook_name_suggestions.

In my node.html.twig I'm attaching a library as normal:

{{ attach_library('anitas/node_styles') }}

but If I use hook_name_suggestions like html--node--1.html.twig

and in the new content insert

{{ attach_library('anitas/node_1_styles') }}

the library is not attaching. I must say that the library syntax is correct. I cannot attach even {{ attach_library('anitas/node_styles') }} in html--node--1.html.twig

What is very strange the library to html--node--1.html.twig is not attaching even I declare it in my some_theme.info.yml. It attaches to any other pages, but not to html--node--1.html.twig

Now I have realized that theme_preprocess_page function don't work in name-suggestion modified page too.

function anitas_preprocess_page(&$variables) {
  $variables['#attached']['library'][] = 'anitas/node_styles';
  dpm($variables);
  drupal_set_message('It is crazy');
}

It lists variables to any other pages, but not in a page I wish :(

It's smells like a bug. Maybe I'm wrong, but what is the way to attach a library to some page only?

Thanks in advance,

George

Comments

George Dramcheff’s picture

Hello and thanks for the reply. I have read this article before I began to work. The problem is that

{{ attach_library('contextual/drupal.contextual-links') }}
<div>Some markup {{ message }}</div>

work on template files, but don't work on the name-suggested templates.

"

In my node.html.twig I'm attaching a library as normal:

{{ attach_library('anitas/node_styles') }}

but If I use hook_name_suggestions like html--node--1.html.twig

and in the new content insert

{{ attach_library('anitas/node_1_styles') }}

the library is not attaching."

Preprocess functions like "themename_preprocess_page" don't attach libraries to name-suggested pages too.

Best regards,

George