Hi community, I need hide block (knowing id 'animalsblock-test') in the pages with the field "animals" with selected "Cats" - how to do it?
My code field validation:

if ($content['field_type_animals'] != NULL) {
    if ($field_type_animals['0']['value'] === 'Cats') {
        //Here it is necessary to hide block
    }
    else {
        //Here it is necessary to show block
    }
}

Comments

jaypan’s picture

I'm assuming that field_type_animals is a field on a node.

Implement hook_block_view_MODULE_DELTA_alter(). Do this:

function hook_block_view_MODULE_DELTA_alter(&$data, $block)
{
  $node = menu_get_object();
  if($node)
  {
    if(isset($node->field_type_animals) && count($node->field_type_animals) && $node->field_type_animals[LANGUAGE_NONE][0]['value'] == 'cats')
    {
      $data = FALSE;
    }
  }
}

Contact me to contract me for D7 -> D10/11 migrations.

vinoth.babu’s picture

For example to Hide a Block for a certain content type

  // Only show if $match is true
  $match = true;

  // Which node types to NOT show block
  $types = array('article', 'blog');

  // Match current node type with array of types
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
    $type = $node->type;
    if(in_array($type, $types)) {$match=false;}
  }
  return $match;