1

Follow-up to the question "How can I show a single block only if the user is on iOS?".

I am using D7 and trying to show a message to iOS users that their devices of choice do not support basic upload functionality. I have a block that I would like to show only on the following path: node/add/profile-picture if the function mymodule_is_iOS() returns TRUE.

On the block configuration screen, I can easily limit it to a given path by adding the path to Show block on specific pages. However, if I do that, I cannot select Pages on which this PHP code returns TRUE to limit it to users for which the function mymodule_is_iOS() returns true.

I found some documentation on Drupal.org here (Combining PHP visibility control with specific page visibility) but it isn't relevant to my case.

Is there a way to do this by adding another function to my custom module for the site or theme? I'm not an expert in PHP but I do have custom functions in a custom module and in my theme.

2

If you have already a custom module, you can implement hook_block_view_alter() using code similar to the following one.

function mymodule_block_view_alter(&$data, $block) {
  if ($block->module == $module_that_creates_the_block && $block->delta == $delta_for_the_block_to_change) {
    if (mymodule_is_iOS() && $_GET['q'] == 'node/add/profile-picture') {
      $data['content'] = '';
      $data['title'] = '<none>';
    }
  }
}

Instead of hook_block_view_alter(), you can implement hook_block_view_MODULE_DELTA_alter() which would be invoked only for a specific block. Checking the block delta, and the module implementing it would not be necessary, with this hook, and the code would be executed only for that block.

Alternatively, you can use the following code when you select the "Pages on which this PHP code returns TRUE" option.

<?php
  return (mymodule_is_iOS() && $_GET['q'] == 'node/add/profile-picture');
?>
4

By enabling core PHP Filter module, you will see another option "Pages on which this PHP code returns TRUE (experts only)" under Show block on specific pages of the block configuration page. Select the option and write the following code in the box.

<?php
  if (mymodule_is_iOS()) {
    if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'profile-picture') {
      return TRUE;
    }
  }
  return FALSE;
?>

Your module must have a lower weight than the core PHP filter module to allow this code snippet to call mymodule_is_iOS().

1

The Rules block visibility module might be another alternative available ... Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

Moreover, as per the "if you can do it with Rules, you can use it to control block visibility" above, you have the power of Rules available to implement all sorts of custom logic. For this specific question, you could specify 2 Rules conditions that must both be satisfied:

  • Condition 1: Verify the current path is what you want it to be.
  • Condition 2: Execute your custom PHP code.

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

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.