In community of developers you can read more than one question about this topic reading a common feed is.
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. Original post here
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.
- 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 create or program a programatic value variant in E-commerce pre-process.
- 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 or IOS and when you create events to an element (image, button, text, variables, services, etc), you can also customize your contents in drupal, in mobile application you can develop functions accord context in your application.
{% if content.field|render is not empty %}
{{ content.field }}
{% endif %}
Note that this can be an expensive (resource-wise) check to make.
{% if node.field.value %}
{{ content.field }}
{% endif %}
{% 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. Use your time to read and understanding answers and questions, another way to resolve problem and not recommend to beginers is use commplements base in IA to find solutions in code, but question yourself time to to learn? time to read and understanding in community? for beginers not a utility. If your beginner work your abstraction and induction!
{% if '1' in content['field_logical]['#items'].getValue()|first.value %}
{#procedures#}
{% endif %}
- 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