0

I have a paragraph with a boolean field.

  • content type
  • -paragraph entity reference
  • left hand side paragraph
  • checkbox
  • image
  • title

All the other fields have their own field-paragraph-field-checkbox.html.twig files and there's nothing to be checked, just adding a class around the image and title {{ item.content }} value.

However, I need to check if the checkbox field value is 1 or 0 in field-paragraph-field-checkbox.html.twig, and add a class if it's 1, or do nothing if it's 0.

When I tried printing the value with {{ content.field_checkbox.value }} nothing prints. I also tried with {{ node.field_checkbox.value }}, but I don't get any value.

How do I check the value of a Boolean field in a template file?

0

I don't know how you debug but I think from question contest that trying node.field_checkbox.0.value might work. Or if you are working with views and use views_get_view_results function, try content._entity.field_checkbox.value or content._entity.field_checkbox.0.value 0 used if field is untranslated

Update for comment:

function TEMPLATE_theme_suggestions_page_alter(&$suggestions, $variables) {

  // Check it's a node page.
  if (($node = \Drupal::routeMatch()->getParameter('node')) && $node instanceof \Drupal\node\NodeInterface) {
    array_splice($suggestions, count($suggestions) - 1, 0, 'page__node__' . $node->getType());
  }
}

with this code in your TEMPLATE.theme file drupal 8 suggest page--node--CONTENT-TYPE-MACHINE-NAME.html.twig (Undersores to dushes). and thats it, You might have {{ kint(node) }} or {{ dump(node) }} in your twig.

  • Hi Alen, tries that in the node.html.twig and field-paragraph-field-checkbox.html.twig files its blank. please see the updated field.html.twig file name maybe that might be the issue, really not sure – Lloyd_07 Jul 31 '18 at 9:42
  • Thus I think you need to suggest the right twig file for your node. Or enable twig debugging to see suggestions in html comments – Alen Simonyan Jul 31 '18 at 9:46
  • I have debug on, and for node template i am using the following. node--home-page.html.twig, which just prints {{ content }} and then for the field template using the field-paragraph-field-checkbox.html.twig – Lloyd_07 Jul 31 '18 at 9:52
  • Hi Alen, i had already added template suggestions in my .theme so page--front.html.twig has {{ page.content }} and node--home-page.html.twig has {{ content }}. kint and debug just keep my site loading nothing prints when i ckear cache and refresh the page – Lloyd_07 Jul 31 '18 at 10:22
  • @Lloyd_07 can you kint $suggestions array in your _theme_suggestions_page_alter – Alen Simonyan Jul 31 '18 at 10:31

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.