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.
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;
}
This solution worked for me, the field_poster
has none or max 1 value.
{% if content.field_poster[0] is not empty %}
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 %}
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.
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
© 2022 - 2024 — McMap. All rights reserved.