0

I have a custom module defining a block which needs some complex page visibility rules that I'll write later. In building out the basic hook_block code the visibility and pages settings don't seem to be working when returning FALSE.

function popup_footer_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0] = array(
      'info' => t('Popup Footer'),
      'visibility' => 2,
      'pages' => popup_footer_block_visibility(),
    );

    return $blocks;
  }
  else if ($op == 'view') {
    switch ($delta) {
      case 0:
        $block = array('content' => 'foo bar');
        break;
    }

    return $block;
  }
}

function popup_footer_block_visibility() {
  return FALSE;
}
0

When the block visibility is set to 2, $block[0]['pages'] needs to be a string that can be passed to drupal_eval() as first argument. In fact, the code which handles the block visibility is the following one.

  // Match path if necessary
  if ($block->pages) {
    if ($block->visibility < 2) {
      $path = drupal_get_path_alias($_GET['q']);
      // Compare with the internal and path alias (if any).
      $page_match = drupal_match_path($path, $block->pages);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages);
      }
      // When $block->visibility has a value of 0, the block is displayed on
      // all pages except those listed in $block->pages. When set to 1, it
      // is displayed only on those pages listed in $block->pages.
      $page_match = !($block->visibility xor $page_match);
    }
    else {
      $page_match = drupal_eval($block->pages);
    }
  }
|improve this answer|||||
  • So it needs a PHP string to interpret that returns boolean value? If I change that function to return 'return false'; it still doesn't work. Thx – Allan Thomas Apr 29 '16 at 23:29
  • return '<?php return false; ?>'; rather – Allan Thomas Apr 30 '16 at 0:06
  • I don't recall if the PHP code requires <?php ?>; the user interface for creating a block says that. It should be one of the following: 'pages' => 'if ($condition) {return FALSE;}' or 'pages' => '<?php if ($condition) {return FALSE;} ?>'. Write the condition you need to make the block not visible instead of $condition. For sure it needs to contain the code to be executed, as string, which is what drupal_eval() expects as its parameter. – kiamlaluno Apr 30 '16 at 6:44
0

'pages' => '<?php return popup_footer_block_visibility(); ?>' worked. Disable the module, delete the module entry from the database, re-enable the module.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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