I have a field added to the content type of basic page. It is a select box, which is either checked or unchecked depending on whether or not the page is of the 'getting started' kind. I want to add the class 'getting-started' to the body element in the html if the page is a getting started page, i.e. the box is checked. I want to do this in the .theme file. So essentially I want to check too see if the field is empty, and if it isn't add the class to the body element. Any help would be much appreciated.
5 Answers
To check if a field is empty you can use built-in method isEmpty()
.
E.g.:
if (!$entity->get('category')->isEmpty()) {
/** @var EntityInterface $category */
$category = $entity->get('category')->entity;
$row['category']['data'] = [
'#type' => 'link',
'#title' => $category->get('name')->value,
'#url' => $category->toUrl(),
];
}
else {
$row['category'] = NULL;
}

- 1,889
- 1
- 18
- 27
This solution worked for me, the field_poster
has none or max 1 value.
{% if content.field_poster[0] is not empty %}

- 3,395
- 8
- 31
- 53

- 81
- 1
- 4
The following way you can check the field is empty or not in TWIG template.
{% if node.field_date.value %}
<div class="my-field-wrapper">
{{ content.field_date }}
</div>
{% endif %}
or
{% if node.field_date.value is not empty %}
{{ content.field_date }}
{% endif %}

- 82
- 2
-
This will only work if this field type has a "value" property. Most don't. – Dalin Aug 08 '20 at 00:50
You can use the template_preprocess_node (https://api.drupal.org/api/drupal/core%21modules%21node%21node.module/function/template_preprocess_node/8) function to pick off the available variables of the node being loaded.
In this function you can access the node's fields like this:
$variables['content']['field_some_name']
If your check passes, you can add some custom variable to use in the twig template:
$variables['something'] = true;
OR: you check the value of the field directly in the node twig template. The question is if it's really necessary for you to add your desired class to the body element. Maybe adding it to the article tag of the node does the job, that would be at least easier to check.
If you really need to add it to the body element, I guess you'll need to use the template_preprocess_page function, and check first if the page being loaded is a node, and then find out which node etc.

- 1,063
- 6
- 11
-
Unfortunately I do have to add the class to the body element, since I need to change the color of the page title depending on the value of the field I'm checking. I'm familiar with the process of adding a class to the body in the 'template_preprocess_page' function, but I'm not familiar with the how's and why's of what you described in the latter part of your response, "check first if the page being loaded is a node and then find out which node etc". – ljolz Jan 19 '16 at 23:06
-
Couldn't I just check to see if the field isset with the code you sampled above, and then apply the class all in the preprocess page function? – ljolz Jan 19 '16 at 23:06
-
The "problem" is that template_preprocess_page is being called on all pages, not only nodes, e.g. on category pages, the front page or any other custom entity pages. So the $variables variable is not always the same. That's what I meant with "check first if the page being loaded is a node, and then find out which node etc.". – Frank Drebin Jan 20 '16 at 09:03
In Drupal 8^9^10
Really goes without saying that to customize you must enter the templates of the specific place to be able to verify if a field is empty or not. But there are different ways to achieve it, in addition the functionality also varies according to the type of project.
Verify that a field is empty with multiple purposes.
- To print or not what comes in the value.
- To act with the value stored in the value of the field.
- To add behavioral js functionality in your project.
- As important data to send in a context.
- To add a specific behavior of the template.
- There are n-utilities that you can give it... Etc.
However, the community publishes the most used cases here a little more according to what you can build, similar when you program in android and when you create events to an element (image, button, text, etc), you can also customize your contents in drupal.
Expensive (render) check
{% if content.field|render is not empty %}
{{ content.field }}
{% endif %}
Note that this can be an expensive (resource-wise) check to make.
Basic check
{% if node.field.value %}
{{ content.field }}
{% endif %}
Entity Reference fields
{% if content.field['#items'].getValue() %}
{{ content.field }}
{% endif %}
Review: Take the trouble to check out the community, to find more ways to solve a problem. Check Stackoverflow account whit similar issue, update information.
Logical filedset
{% if '1' in content['field_logical]['#items'].getValue()|first.value %}
{#procedures#}
{% endif %}
Happy code!
Review this links for community
- https://www.drupal.org/forum/support/theme-development/2016-03-18/how-to-check-if-a-field-value-is-empty
- https://drupal.stackexchange.com/questions/198570/verify-a-field-is-not-empty
- https://www.previousnext.com.au/blog/right-way-check-empty-content-twig
- https://www.drupaleasy.com/quicktips/checking-existence-field-value-twig
- https://drupal.gatech.edu/handbook/check-non-empty-fields