I have a custom block type with two different fields. What I am trying to accomplish is, to hide the first field if the second is not empty.

That seems like something that could be done in template_preprocess, but where would I put it and what would be my template?



you can utilize twig's if/else condition:

{% if field_NAME_OF_YOUR_SECOND_FIELD.0 == false %}
    {{ field_NAME_OF_YOUR_FIRST_FIELD.0 }}
{% endif %}

check this for extracting the values of different fields


Preprocessing the block_content template was considered undesireable, and they suppressed the functionality. There is a module on github that provides this functionality, but since it lacks composer support, you might just copy it into your project.

All that said, you should find that template_preprocess_block allows access to what you want, and this might work:

function MYTHEME_preprocess_block(&$variables) {

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.