1

Heres my code:

$blocks['membership_package_information'] = array( 
    'info' => "Membership Package Information",
    'visibility' => BLOCK_VISIBILITY_PHP,
    'pages' => membership_block_visibility(),
  );

function membership_block_visibility() {
  if(arg(0) == 'checkout' && arg(2) == null) {
    return true;
  }
  else {
    return false;
  }
}

I'm trying to limit the block to only show on the first page of the drupal commerce checkout page (checkout/[order-id]) and not the second (checkout/[order-id]/review) but its not working. When I put this code in the PHP visibility section of the block UI page, it works. I'd rather it be in my module, why wont it work? It seems that the UI settings are being picked up and not whats in my block.

1 Answer 1

3

Re: https://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_info/7 you have to put the actual PHP code "in" the pages array key...

For BLOCK_VISIBILITY_PHP, the PHP code's return value should be TRUE if the block is to be made visible or FALSE if the block should not be visible.

...not set it to the return of a function, eg, something along these lines:

$blocks['membership_package_information'] = array( 
    'info' => "Membership Package Information",
    'visibility' => BLOCK_VISIBILITY_PHP,
    'pages' => '<?php return membership_block_visibility(); ?>' // ADDITION: you need the <?php ?> brackets :)
);

// make sure this function is in a module file that is always loaded...

function membership_block_visibility() {
  if(arg(0) == 'checkout' && arg(2) == null) {
    return true;
  }
  else {
    return false;
  }
}

Do also note the caveat off the api page:

Most modules do not provide an initial value for 'visibility' or 'pages', and any value provided can be modified by a user on the block configuration screen.

10
  • I tried this, and removed all of my php code from the UI php section, but it didn't work - now the blocks display on all pages aka- its not picking up my code! Commented Nov 18, 2013 at 20:24
  • I even tried using this line 'pages' => 'return false;', and the blocks still show. Commented Nov 18, 2013 at 20:31
  • the block module checks to make sure that you have the php module installed so it can php_eval() the contents of 'pages'. I'd IMAGINE you couldn't have the PHP option in the UI without it, but just make sure it is installed and turned on, etc.
    – Jimajamma
    Commented Nov 18, 2013 at 20:32
  • going to the block ui should clear all the caches necessary but that might be an issue, so try the every present clear your caches suggestion. also sometimes things are only run when a module is first installed, so you may have to disable, uninstall, and re-enable your module for everything to get re-synced.
    – Jimajamma
    Commented Nov 18, 2013 at 20:35
  • 1
    the block module relies upon hook_modules_uninstalled() to clean up its tables when another module is uninstalled so your uninstall/re enable would put everything back to square one for certain.
    – Jimajamma
    Commented Nov 18, 2013 at 20:42

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.