3

I am a newbie. I have experience working with Twig but in Drupal it is used in another way.

I have seen a lot of code like that

<section{{ attributes.addClass(classes) }}>

And of course I read this article https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates

There are a lot of examples and that is great.

But why do I need this, how could I use them to improve readability and flexebility ?

You should agree that attributes.addClass(classes) method is doing something like adding class to the existing attributes, but not returning the output of all attributes.

Could you tell me please why this attributes variable is useful, how can I use it to provide better decoupling or whatever it is used for. Why not just to keep classes directly in a template file, is it possible to add them from admin panel ? Thanks

1 Answer 1

2

A general reason is that contributed modules or theme code can hook in and add attributes in. Outputting {{ attributes }} ensures that they are all printed without hardcoding them all in templates.

Here is an example.

I want to add a class and data-attr to an input field on a particular form. Instead of hardcoding it, I add to the #attributes key, and they are rendered out for me since {{ attributes }} is printed on the element:

/**
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
 */
function mytheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-search-results') {  
    $form['actions']['submit']['#attributes']['data-twig-suggestion'] = 'search_results_submit';
    $form['actions']['submit']['#attributes']['class'][] = 'search-box__button';
  }
}

My class and data-attr are added to that element automatically. I don't need to know what twig file is rendering it, I just need to provide the attributes I want to add on to, or print out.

Here are some links that explain a bit more in depth what Attributes are:

I heard that in 8.3.x it will be easier to work with attributes, but I can't find the thread offhand.

6
  • Thanks for the answer, is it possible to add attributes from admin panel? For example id, data-attr ? I understand that it is possible to do this using the alter function, but I am interested if I can set for example data-article-id in the admin panel, and it will be automatically added to the attributes variable ? Commented Mar 1, 2017 at 16:19
  • It would be the job of a contributed module to provide such a functionality in the UI, for example, the Menu Link Attributes module does this for menu links. drupal.org/project/menu_link_attributes
    – Kevin
    Commented Mar 1, 2017 at 16:20
  • thanks, so basically it is used for an _alter method, any module could alter attributes and add specific ones ? Commented Mar 1, 2017 at 16:21
  • That isn't exactly what an Attribute is per se, but yes, generally you can add on to them if you need to.
    – Kevin
    Commented Mar 1, 2017 at 16:23
  • Thaks, maybe you could add some more usage examples, I would be grateful. Because now it seems not so useful for me, because in most cases classes are just added at the beginning of a template file. Commented Mar 1, 2017 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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