Looking for a way to preprocess two fields. I am trying to hide Field 1 from display if Field 2 has value.

If Field 1 has value and Field 2 has value display Field 2 only. If Field 1 has value and Field 2 has NO value display Field 1.

This should only apply to content type USER POSTS. And I am using Display Suite to arrange the fields if that makes a difference.

What would be the preprocess hook to place on mytheme.theme?

Any help is greatly appreciated.


Can you hide it with CSS? In which case you could do something simple like:


 * Implements template_preprocess_hook().
 * Adapt logic, field names etc to suit your needs.
function MYTHEME_preprocess_page(&$variables) {

  if (empty($variables['node']->get('my_field_2')->value)) {
    $variables['page_classes']['#plain_text'] .= ' field_two--display-none';


/* CSS */
.field_two--display-none { display: none; }

Not super elegant but would do the trick.

Your other option is to use the conditional fields module.

  • Can you help me understand the if statement? Does it say if my_field_2 is empty then set this CSS class or if my_field_2 has value then set this CSS class? – Progonat Oct 16 '18 at 14:05
  • 1
    It's asking: Does $variables['node']->get('my_field_2')->value return an empty value? If so, then apply the CSS class to hide this field. Hope that makes sense! – 100pic Oct 16 '18 at 23:21
  • This needs a little bit of tweak and I am stuck. The logic I am trying to implement is: If field_2 retruns value (is not null), apply hide css to field_1. Example: Field 2 is the video, if video exist hide field 1 which is the image. Any tips? – Progonat Nov 5 '18 at 16:53

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.