You are here

function yr_verdata_block_view in Yr Weatherdata 7

Implementation of hook_block_view().

File

./yr_verdata.module, line 339
yr_verdata.module This file contains the code for getting the forecast from yr.no and displaying it on a Drupal site.

Code

function yr_verdata_block_view($delta = '') {

  // Create the regular block, listing all locations.
  if ($delta == 'yr_verdata_block') {
    $block['subject'] = l(t('Forecast'), 'forecast');
    $block['content'] = array();

    // Load locations.
    $records = _yr_verdata_get_all(TRUE);
    if (count($records) == 0) {
      return array();
    }
    foreach ($records as $record) {
      $load = yr_verdata_load_location($record->yid);
      $location = $load['data'];
      $name = yr_verdata_resolve_name($record);

      // Unclean, but cleaned by yr_verdata_generate_forecastboxes().
      $block['content'][]['#markup'] = yr_verdata_generate($location, 'block', $name);
    }
    $block['content'][]['#markup'] = yr_verdata_credit_link(FALSE, TRUE);

    // Add CSS.
    $block['content']['#attached'] = array(
      'css' => array(
        drupal_get_path('module', 'yr_verdata') . '/yr_verdata.css',
      ),
    );
  }
  elseif ($delta == 'yr_verdata_randomblock' && variable_get('yr_verdata_randomblock', 'off') == 'on') {

    // If the random block is enabled and placed in a region.
    $load = yr_verdata_load_location(-1);
    $location = $load['data'];
    $name = yr_verdata_resolve_name($location);

    // Unclean, but cleaned by l().
    $block['subject'] = l(t('Forecast for !location', array(
      '!location' => $name,
    )), 'forecast/' . $location->yid);
    $block['content'] = array(
      '#markup' => yr_verdata_generate($location, 'block') . yr_verdata_credit_link($location->url, TRUE),
      '#attached' => array(
        'css' => array(
          drupal_get_path('module', 'yr_verdata') . '/yr_verdata.css',
        ),
      ),
      '#cache' => array(
        'keys' => array(
          'yr_verdata',
          'block',
          'random',
          $location->yid,
        ),
        'bin' => 'cache',
        'expire' => REQUEST_TIME + variable_get('yr_verdata_maxage', 21600),
      ),
    );
  }
  else {

    // Create a block for a given location.
    // Check for the multiblock setting here as well as in hook_block_info(),
    // because otherwise if 'yr_multiblocks' is swithced off while multiblocks
    // are assigned to regions, they won't show up on the administrative 'blocks'
    // page, but still be visible in whatever region they were assigned to.
    if (variable_get('yr_verdata_multiblocks', 'off') == 'on') {

      // Get the yid from the delta.
      preg_match('{(\\d+)}', $delta, $m);

      // Load the location.
      $load = yr_verdata_load_location($m[1]);
      $location = $load['data'];
      if ($load['status'] == TRUE) {
        $name = yr_verdata_resolve_name($location);

        // Unclean, but cleaned by l().
        $block['subject'] = l(t('Forecast for !location', array(
          '!location' => $name,
        )), 'forecast/' . $location->yid);
        $block['content'] = array();
        $block['content']['#markup'] = yr_verdata_generate($location, 'block') . yr_verdata_credit_link($location->url, TRUE);
        $block['content']['#attached'] = array(
          'css' => array(
            drupal_get_path('module', 'yr_verdata') . '/yr_verdata.css',
          ),
        );
      }
      else {
        $block = array();
      }
    }
  }
  return $block;
}