Drupal 7: Hide a field depending on the value of a checkbox

User login

I recently had the requirement to hide a horizontal tab in node display depending on the value of a checkbox i.e. Show tab X. For the horizontal tabs I was using the excellent Fieldgroup module.

In order to achieve the above, I created a little custom module and used the newly introduced in drupal 7 hook, hook_node_view_alter.

Here is the code that checks the value of the field, then unsets it. Please note that the checkbox needs to be included in the node display settings in order for it to become available in the $build variable. There is an extra check for isset, just to avoid some PHP notices.

function MYMODULE_node_view_alter(&$build) {
  if (isset(
$build['field_checkbox']['#items'][0]['value'] )) {
    if (
$build['field_field_checkbox']['#items'][0]['value'] == 0 ) {

Once I unset the contents of the field, the horizontal tab was removed, as this is the default behavior when there is an empty field in tab's contents.

Drupal 7