I am working on a contrib theme and would like the change the markup of a block depending upon which region it is in. 

I can not figure out how to check for the region - or if this is possible?

Is it possible to do something like this in the custom block.tpl.php file?

<?php if ($variables['region'] == 'header'): ?>

Accepted answer

@argiepiano - Thanks that final suggestion worked. Here it is all together.

in template.php

function opera_preprocess_block(&$variables) {
  $uuid = $variables['block']->uuid;
  $variables['region'] = $variables['layout']->getBlockPosition($uuid);
}

in block.tpl.php

  <?php if ($region == 'header'): ?>
    <div class="block--inner-wrapper container">
  <?php else: ?>
    <div class="block--inner-wrapper">
  <?php endif; ?>

Big thanks!

Comments

I dont think its possible, since blocks are rendered to HTML before the regions are assembled (in the layout renderer). You'd need javascript, depending on what youre trying to do.

It is possible. You need to implement template_preprocess_block and pass the region as a variable. For example, you can create a small custom module called "my_module" and write the following function.

function my_module_preprocess_block(&$variables) {
  $uuid = $variables['block']->uuid;
  $variables['region'] = $variables['layout']->getBlockPosition($uuid);
}

Then, in block.tpl.php you'll be able to use the variable $region, which will have the machine name of the region where the block is located.

EDIT: in you example, after the above, you'd do:

<?php if ($region == 'header'): ?>

EDIT 2: And since you are writing a custom theme, it'd be easier to place the function within template.php and call it THEME_NAME_preprocess_block, or edit an existing THEME_preprocess_block in that file.

Thanks @argiepiano

I've added this to my template.php file and I see messages indicating that the region is found and stored to `$variables['region']`

function opera_preprocess_block($variables) {
  $uuid = $variables['block']->uuid;
  $variables['region'] = $variables['layout']->getBlockPosition($uuid);
  backdrop_set_message($variables['region']);
}

However, I can't seem to access that variable in my block.tpl.php file. I've tried the following (to debug/test) in block.tpl.php.

  <?php print $variables['id']; ?>
  <?php print $variables['region']; ?>
  <?php print $region; ?>

I can access $variables['id'], but nothing to do with the region. How do I pass this variable from template.php to block.tpl.php?

What am I missing?

EDIT: I suspect that at least part of my problem is that I'm finding the region for all the blocks in hook_preprocess_block, but I only need to access the region for a single block. 

Tim, the preprocess function does run once for each of the blocks on the page, so you are correctly seeing it run multiple times.  But the correct region for each block will be passed to the template file individually every time a block is rendered.

This should work. I'll take a look a bit later and will respond via Zulip.

OK, I see the problem now. It's a typo in my message

Please be sure to accept the parameter BY REFERENCE. The function should be:

function opera_preprocess_block(&$variables)

NOTICE the "&" before the "$"

@argiepiano - Thanks that final suggestion worked. Here it is all together.

in template.php

function opera_preprocess_block(&$variables) {
  $uuid = $variables['block']->uuid;
  $variables['region'] = $variables['layout']->getBlockPosition($uuid);
}

in block.tpl.php

  <?php if ($region == 'header'): ?>
    <div class="block--inner-wrapper container">
  <?php else: ?>
    <div class="block--inner-wrapper">
  <?php endif; ?>

Big thanks!