wearewondrous / fractal-twig-drupal-adapter

Twig template adapter for Fractal with Drupal 8 directives.
http://fractal.build
MIT License
13 stars 13 forks source link

Add support for drupal attributes in configuration files #28

Open David-Czinege opened 4 years ago

David-Czinege commented 4 years ago

I think it should be good, if we can add predefined attributes in config files easily, which are accessible in twig templates as Drupal's attributes object.

Drupal's attribute usage in twig: https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates

There is a JavaScript port of the Drupal's Attribute object, which can be used: https://www.npmjs.com/package/drupal-attribute

It will be good, if all the context values, where the key start with '_' character and ends with '_attributes' are converted into drupal attribute.

So I would like something like this:

With this config.yml:

title: Checkbox
label: Checkbox
status: ready
notes: 'Custom checkbox'
tags: ['checkbox', 'form']
context:
  _wrapper_attributes: {}
  _attributes:
    class:
      - custom-checkbox
      - another-class
    name: checkbox
    id: checkbox
    type: checkbox
  id: checkbox
  label: Regular Option

And with this twig template:

<div{{ wrapper_attributes.addClass('c-checkbox') }}>
  <input{{ attributes.addClass('c-check') }}>
  <label for="{{ id }}">{{ label }}</label>
</div>

I get this HTML output:

<div class="c-checkbox">
    <input class="custom-checkbox another-class c-check" name="checkbox" id="checkbox" type="checkbox">
    <label for="checkbox">Regular Option</label>
</div>