nuvoleweb / ui_patterns

[NOTE] Development has moved to https://drupal.org/project/ui_patterns
https://drupal.org/project/ui_patterns
GNU General Public License v2.0
85 stars 56 forks source link

Define HTML element classes with pattern fields. #241

Closed laurencefass closed 5 years ago

laurencefass commented 5 years ago

I want to use drupal fields to define class names in ui_patterns. i.e. define a field in a pattern twig.html file and use its {{ twig variable }} as a class name. The following doesn't work. Is there a correct way of doing this? even possible?

{% set classes = [ 'class_name_1', 'class_name_2', {{ ui_pattern_field_name }}, ] %} <div{{ attributes.addClass(classes) }}>

laurencefass commented 5 years ago

actual example i've mapped a string select box to

yaml pattern field:
... pattern_field: type: 'text' label: 'Entry position' description: 'Set overlay entrance position (top, bottom, left, right, fade)' preview: 'entry-right'

html.twig file: {% set classes= ['custom_class', pattern_field] %} <div {{ attributes.addClass(classes) }}>

results: {{ pattern_field }} outputs select list selection works ok. html output replaced pattern_field with 'Array'

Drupal UI Notice: Array to string conversion in Drupal\Core\Template\AttributeArray->__toString() (line 73 of core/lib/Drupal/Core/Template/AttributeArray.php).

laurencefass commented 5 years ago

discovered this is a more general question about drupal and twig.