You are here

function headerimage_select_node in Header image 7

Same name and namespace in other branches
  1. 5 headerimage.module \headerimage_select_node()
  2. 6 headerimage.module \headerimage_select_node()

Select a node to be displayed in the block

Node selection by (1)weight and (2)condition. If multiple conditions are present, any true condition will select the node. If no node is selected by the conditions and random fallback selection is enabled for the block, one of the available nodes will be selected at random.

Parameters

$block: The headerimage block number ($delta)

Return value

nid of the selected node null: no node selected

2 calls to headerimage_select_node()
headerimage_block_view in ./headerimage.module
Implementation of hook_block_view().
views_plugin_argument_default_headerimage::get_argument in views/views_plugin_argument_default_headerimage.inc
Return the default argument.

File

./headerimage.module, line 244
headerimage.module Conditionally display an node in a block.

Code

function headerimage_select_node($block) {
  $match = NULL;
  $block_nids = array();
  $query = db_select('headerimage', 'hi');
  $query
    ->fields('hi', array(
    'nid',
    'conditions',
  ));
  $query
    ->condition('hi.block', $block);
  if (module_exists('locale') && variable_get('headerimage_language_support', FALSE)) {
    $query
      ->join('node', 'n', 'hi.nid = n.nid');

    // Respect language neutral if set
    if (variable_get('headerimage_language_support_neutral', FALSE)) {
      $query
        ->condition(db_or()
        ->condition('n.language', $GLOBALS['language']->language)
        ->condition('n.language', LANGUAGE_NONE));
    }
    else {
      $query
        ->condition('n.language', $GLOBALS['language']->language);
    }
  }
  $query
    ->orderBy('hi.weight', 'ASC');
  $query
    ->orderBy('hi.nid', 'ASC');
  $result = $query
    ->execute();
  foreach ($result as $header_image) {
    $conditions = unserialize($header_image->conditions);
    $match = false;

    // Store the nid in an array for the random selection fallback option.
    $block_nids[] = $header_image->nid;
    $selected_types = variable_get('headerimage_condition_types', array(
      'nid' => 'nid',
    ));
    foreach ($conditions as $type => $condition) {
      if (!empty($condition) && !empty($selected_types[$type])) {
        switch ($type) {
          case 'nid':
            $match = headerimage_eval_nid($condition);
            break;
          case 'url':
            $match = headerimage_eval_url($condition);
            break;
          case 'taxonomy':
            $match = headerimage_eval_taxonomy($condition);
            break;
          case 'book':
            $match = headerimage_eval_book($condition);
            break;
          case 'nodetype':
            $match = headerimage_eval_nodetype($condition);
            break;
          case 'php':
            $match = php_eval($condition);
            break;
        }
      }
      if ($match) {
        break;
      }
    }
    if ($match) {
      break;
    }
  }
  if ($match) {
    return $header_image->nid;
  }
  elseif (variable_get('headerimage_block_' . $block . '_random_fallback', 0) && count($block_nids)) {
    return $block_nids[array_rand($block_nids)];
  }
}