Skip to content

Instantly share code, notes, and snippets.

@draven3166
Last active September 7, 2023 23:17
Show Gist options
  • Save draven3166/3d3f8ca5441f95a0ba890aa483ae3b74 to your computer and use it in GitHub Desktop.
Save draven3166/3d3f8ca5441f95a0ba890aa483ae3b74 to your computer and use it in GitHub Desktop.
How do you check if a field is empty in drupal 8^9^10 ?

How do you check if a field is empty in drupal 8^9^10 on Twig ?

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

In development you have more than one reason to use or verify this

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 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.

Explaining a code

< Let's Code />

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. 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!

Logical fieldset

{% if '1' in content['field_logical]['#items'].getValue()|first.value %}
  {#procedures#}
{% endif %}

< Happy code! />

Review this links for community

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment