Skip to main content
added 41 characters in body
Source Link
Watergate
  • 1.8k
  • 14
  • 15

Although it is of course possible to implement this kind of logic in Twig (check Hudri's comment of your question), but I would opt for using a preprocess function. In addition, I also recommend to add an extra class attribute, instead of adding an inline style. From my experience, implementing the functionality in this way provides a lot more flexibility. In your (sub-)theme's .theme or custom module's .module file, you can add something like:

/**
 * Implements hook_preprocess_field() for field_rating.
 */
function THEMENAME_preprocess_field__field_rating(&$variables) {
  foreach ($variables['items'] as &$item) {
    if ($item['#key_of_the_value_you_want_to_check'] > 5) {
      $item['content']['#attributes']['class'][] = 'extra_class_attribute';
    }
  }
}

In your css (or Sass) you can then assign the styling:

.extra_class_attribute {
  background-color: green;
}

In the case you needed a wrapping element around your field, you can set the #prefix and #suffix properties of the render array (instead of the #attributes).

Although it is of course possible to implement this kind of logic in Twig, but I would opt for using a preprocess function. In addition, I also recommend to add an extra class attribute, instead of adding an inline style. From my experience, implementing the functionality in this way provides a lot more flexibility. In your (sub-)theme's .theme or custom module's .module file, you can add something like:

/**
 * Implements hook_preprocess_field() for field_rating.
 */
function THEMENAME_preprocess_field__field_rating(&$variables) {
  foreach ($variables['items'] as &$item) {
    if ($item['#key_of_the_value_you_want_to_check'] > 5) {
      $item['content']['#attributes']['class'][] = 'extra_class_attribute';
    }
  }
}

In your css (or Sass) you can then assign the styling:

.extra_class_attribute {
  background-color: green;
}

In the case you needed a wrapping element around your field, you can set the #prefix and #suffix properties of the render array (instead of the #attributes).

Although it is of course possible to implement this kind of logic in Twig (check Hudri's comment of your question), but I would opt for using a preprocess function. In addition, I also recommend to add an extra class attribute, instead of adding an inline style. From my experience, implementing the functionality in this way provides a lot more flexibility. In your (sub-)theme's .theme or custom module's .module file, you can add something like:

/**
 * Implements hook_preprocess_field() for field_rating.
 */
function THEMENAME_preprocess_field__field_rating(&$variables) {
  foreach ($variables['items'] as &$item) {
    if ($item['#key_of_the_value_you_want_to_check'] > 5) {
      $item['content']['#attributes']['class'][] = 'extra_class_attribute';
    }
  }
}

In your css (or Sass) you can then assign the styling:

.extra_class_attribute {
  background-color: green;
}

In the case you needed a wrapping element around your field, you can set the #prefix and #suffix properties of the render array (instead of the #attributes).

Source Link
Watergate
  • 1.8k
  • 14
  • 15

Although it is of course possible to implement this kind of logic in Twig, but I would opt for using a preprocess function. In addition, I also recommend to add an extra class attribute, instead of adding an inline style. From my experience, implementing the functionality in this way provides a lot more flexibility. In your (sub-)theme's .theme or custom module's .module file, you can add something like:

/**
 * Implements hook_preprocess_field() for field_rating.
 */
function THEMENAME_preprocess_field__field_rating(&$variables) {
  foreach ($variables['items'] as &$item) {
    if ($item['#key_of_the_value_you_want_to_check'] > 5) {
      $item['content']['#attributes']['class'][] = 'extra_class_attribute';
    }
  }
}

In your css (or Sass) you can then assign the styling:

.extra_class_attribute {
  background-color: green;
}

In the case you needed a wrapping element around your field, you can set the #prefix and #suffix properties of the render array (instead of the #attributes).