How can I show a block on a specific content type programmatically? I want in my image the 'basic page' selected.

enter image description here

I used the xxx_block_info() function, should it set in here?

2 Answers 2


If you look at the node_module file it controls the node type visibility using the hook_block_list_alter.

So basically it might looks something like this. NOTE: This is a non tested version.

function hook_block_list_alter(&$blocks) {
  $node = menu_get_object();
  // list of all block deltas u want to apply this 
  $block_delta = array('deltas');
  foreach ($blocks as $key => $block) {
     if (!empty($node) && $node->type == 'page' && in_array($key, $block_delta)) {
  • There is anyway in hook_block_list_alter to get the content_types choosed in the block config page? Commented Aug 1, 2014 at 12:20

You can do like this:

function xxx_block_info() {
    $blocks['xxx'] = array(
    'info' => t('Xxx'),
    'visibility' => BLOCK_VISIBILITY_PHP,
    'pages' => '<?php global $node; if ($node->type == 'yyy') return TRUE; else return FALSE; ?>'

Verify that the PHP filter module is enabled if you want to use BLOCK_VISIBILITY_PHP!

  • PHP filter is a VERY BAD IDEA and should probably have a dependency on Bad Judgement
    – beth
    Commented Apr 1, 2014 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.