Blog

Conditional Fields and Display Suite

When Display Suite builds the entity for rendering it only has the fields that are part of the display mode. So if you exclude the Conditional Field it won't show up in the entity.

As part of our normal Drupal process we use both Display Suite (DS) and Conditional Fields frequently. While building out a site I ran into a very strange issue, when I was trying out output a field through DS it just wasn’t showing up. I tried playing around with the formatters, label, etc. … and nothing changed. So I fired up xdebug and took a look at the render array coming through and, yep, the field was in there. But ‘#access’ was set to false. I went through all the usual suspects, user permissions, field permissions … nothing. When the entity was loaded it was fine, but just prior to rendering it was set to false. So when stepping through all the code I ran into the culprit&helip; conditional_fields_entity_view_alter

When Display Suite builds the entity for rendering it only has the fields that are part of the display mode. So if you exclude the Conditional Field it won’t show up in the entity. And in this little snippet of code:

// Manage orphan fields (dependents with no dependees).
$evaluate = in_array(CONDITIONAL_FIELDS_FIELD_VIEW_EVALUATE, $behaviors);
$hide_orphan = in_array(CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_ORPHAN, $behaviors);
$hide_untriggered_orphan = in_array(
  CONDITIONAL_FIELDS_FIELD_VIEW_HIDE_UNTRIGGERED_ORPHAN, behaviors);

$is_orphan = empty($build[$dependee]['#access']);

if ($is_orphan) {
  // Hide the dependent. No need to evaluate the dependency.
  if ($hide_orphan) {
    $build[$dependent]['#access'] = FALSE;
    continue;
  }
}

It directly checks to see if the conditional field is in the entity, and if not sets the ‘#access’ attribute to false. So we just made a change to the information architecture to have that field display on the page and everything worked. Once you know it’s a fairly simple thing to understand. But if you haven’t run into it before it can take some significant investigation to figure it out.