https://www.scottsawyerconsulting.com/blog/Component-Driven-Drupal-Them…
hasField('field_button_color') && !$paragraph->field_button_color->isEmpty()) { $variables['attributes']['class'][] = 'button-color--' . $paragraph->field_button_color->value; } }
or in
** * Prepares variables for entity template. * * Default template: items.html.twig. * * @param array $variables * An associative array of variables. */ function template_preprocess_entity(&$variables) { $field_attribute_mapping = [ 'field_placement' => 'overlay-placement', ]; foreach ($field_attribute_mapping as $field_name => $attribute) { if (!$entity->hasField($field_name)) { continue; } if ($entity->get($field_name)->isEmpty()) { continue; } if ($value = $entity->get($field_name)->target_id) { // This is an entity reference field. Get the entity label. // Get the related entity (taxonomy term). if ($related_entity = $entity->get($field_name)->entity) { $entity_label = $related_entity->label(); $value = \Drupal\Component\Utility\Html::cleanCssIdentifier($entity_label); } else { // Fall back to entity ID retrieved above. } } else { $value = $entity->get($field_name)->value; } $variables['attributes']['class'] = "{$attribute}--{$value}"; $variables['attributes']["data-{$attribute}"] = $value; } }