I have a selectlist-field based bg-colors:

  • yellow
  • black
  • blue

I added this to paragraph bundle called 'Hero'. Now i got selectlist-field on this paragraph bundle. How do I print that yellow to paragraphs-item template? I tried it few times but it always returned 1.

Any ideas?


You need to use a preprocess hook to add a class / set a style to the paragraph based on the field content.

Here's some sample code from one of my sites that uses this. The options for bg_color field are like: "Ghost Gray" => "eeeeee" and the like. The paragraph type is colorsection.

function my_module_preprocess_paragraph__colorsection(&$variables) {
  if (!$variables['paragraph']->field_bg_color->isEmpty()) {
    $variables['attributes']['style'][] = 'background: #'.$variables['paragraph']->field_bg_color->value.';';
    $variables['attributes']['class'][] = 'bg-'.$variables['paragraph']->field_bg_color->value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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