The attributes are wildly used throughout the theme functions and I think they should be available and converted to Attribute() class automatically directly in the theme engine so the developer won' need to define and transfomr them each time a new theme functions is created.

As a developer I would really like this feature.

Comments

star-szr’s picture

Status: Active » Closed (cannot reproduce)
Issue tags: +Twig
Related issues: +#2108771: Remove special cased title_attributes and content_attributes for Attribute creation

Hi @ivanjaros, core already does both of these things for you.

_template_preprocess_default_variables() sets up the 'attributes' variable, as well as title_attributes and content_attributes.

The code below from \Drupal\Core\Theme\ThemeManager::theme() initializes the variables as Attribute objects. More specifically, during preprocess the variables are just arrays of attributes similar to D7, and these arrays are converted to Attribute objects before being passed to the template:

      if (!isset($default_attributes)) {
        $default_attributes = new Attribute();
      }
      foreach (array('attributes', 'title_attributes', 'content_attributes') as $key) {
        if (isset($variables[$key]) && !($variables[$key] instanceof Attribute)) {
          if ($variables[$key]) {
            $variables[$key] = new Attribute($variables[$key]);
          }
          else {
            // Create empty attributes.
            $variables[$key] = clone $default_attributes;
          }
        }
      }
Anonymous’s picture

Great!