Skip to content

Instantly share code, notes, and snippets.

@Morasta
Last active September 2, 2016 02:10
Show Gist options
  • Save Morasta/4e07210209e15e1c4ae5 to your computer and use it in GitHub Desktop.
Save Morasta/4e07210209e15e1c4ae5 to your computer and use it in GitHub Desktop.
Drupal 7 - Hide labels for empty display suite fields
/**
* Implements hook_field_attach_view_alter().
*
* Hide label of display suite fields if field content is empty.
*/
function cof_directory_field_attach_view_alter(&$output, $context) {
$displaySuiteFields = array("graduate_students","office_location");
foreach ($output as $field_name => $field) {
if (!empty($field['#label_display'])) {
$fieldText = "1";
if (in_array($field_name, $displaySuiteFields)) {
$fieldText = trim(strip_tags($field[0]['#markup']));
}
if(empty($fieldText) || (isset($field[0]['#markup']) && empty($field[0]['#markup']) && ($field[0]['#markup'] !== 0))) {
unset($output[$field_name]);
}
}
}
}
@Morasta
Copy link
Author

Morasta commented Mar 16, 2015

Updated to hide the entire field so that the empty divs won't be rendered.

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