You are here

function block_country_block_list_alter in Block Country 7

Implements hook_block_list_alter().

Check country visibility for block Remove block if block is not visible for specific country.

File

./block_country.module, line 183
Country_Block - Module for showing the country specific blocks.

Code

function block_country_block_list_alter(&$blocks) {
  foreach ($blocks as $key => $block) {

    // Check Country settings is enabled for block.
    if (!$block->country_visiblility) {

      // If country settings enabled for a block then validate block visibility
      // in current country.
      $country_query = db_select('block_country', 'cb');
      $country_query
        ->condition('cb.module', $block->module, '=')
        ->condition('cb.delta', $block->delta, '=')
        ->fields('cb', array(
        'country_code',
      ));
      $country_list = $country_query
        ->execute()
        ->fetchAllKeyed(0, 0);

      // Remove block if block is not visible for specific country.
      if (!in_array($_SESSION['user_country_code'], $country_list)) {
        unset($blocks[$key]);
      }
    }
  }
}