You are here

function headerimage_select_node in Header image 5

Same name and namespace in other branches
  1. 6 headerimage.module \headerimage_select_node()
  2. 7 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

1 call to headerimage_select_node()
headerimage_block in ./headerimage.module
Implementation of hook_block().

File

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

Code

function headerimage_select_node($block) {
  $result = db_query("SELECT nid, conditions FROM {headerimage} WHERE block = %d ORDER BY weight, nid ASC", $block);
  while ($header_image = db_fetch_object($result)) {
    $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 = drupal_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)];
  }
}