You are here

function _blockreference_potential_references_standard in Block reference 7

Build an array of all candidate referenced blocks.

Parameters

$field: Array containing field data.

$current_bids: Array of current bids.

$return_full_blocks: Whether to return full blocks.

$string: Filter string to match blocks.

$exact_string: Strictly match string like for validation.

Return value

An array of whatever is needed.

1 call to _blockreference_potential_references_standard()
_blockreference_potential_references in ./blockreference.module
Fetch an array of all candidate referenced blocks.

File

./blockreference.module, line 538
Defines a field type for referencing a block from a node.

Code

function _blockreference_potential_references_standard($field, $current_bids = array(), $return_full_blocks = FALSE, $string = '', $exact_string = FALSE) {
  static $block_info = array();
  $related_regions = array();
  $related_modules = array();
  $related_clauses = array();
  $args = array();
  $settings =& $field['settings'];

  // Handle related regions - this will be used in extra query conditions.
  if (isset($settings['referenceable_regions']) && is_array($settings['referenceable_regions'])) {
    $settings['referenceable_regions'] = array_filter($settings['referenceable_regions']);
    foreach ($settings['referenceable_regions'] as $key => $related_region) {
      if ($related_region) {
        $placeholder = ':region_' . $key;
        $related_regions[] = 'region = ' . $placeholder;
        $args[$placeholder] = $related_region;
      }
    }
  }
  if (!empty($related_regions)) {
    $related_clauses[] = implode(' OR ', $related_regions);
  }

  // Handle related modules - this will be used in extra query conditions.
  if (isset($settings['referenceable_modules']) && is_array($settings['referenceable_modules'])) {
    $settings['referenceable_modules'] = array_filter($settings['referenceable_modules']);
    foreach ($settings['referenceable_modules'] as $key => $related_module) {
      if ($related_module) {
        $placeholder = ':module_' . $key;
        $related_modules[] = 'module = ' . $placeholder;
        $args[$placeholder] = $related_module;
      }
    }
  }
  if (!empty($related_modules)) {
    $related_clauses[] = implode(' OR ', $related_modules);
  }

  // Assemble the extra query condition.
  $related_operator = !empty($settings['referenceable_operator']) ? $settings['referenceable_operator'] : 'AND';
  $related_clause = !empty($related_clauses) ? ' AND ((' . implode(') ' . $related_operator . ' (', $related_clauses) . '))' : '';
  $args[':themes'] = blockreference_field_referenceable_themes($field);

  // Assemble the query.
  $result = db_query('SELECT bid, module, delta, title, region, theme ' . 'FROM {block} ' . "WHERE theme IN (:themes) " . $related_clause, $args);

  // Execute the query, test each row, and return the blocks or block info.
  $rows = array();
  foreach ($result as $block) {
    if (count($args[':themes']) <= 1) {

      // If there were not multiple themes, don't track the theme.
      unset($block->theme);
    }
    if (!isset($block_info[$block->module])) {
      $block_info[$block->module] = module_invoke($block->module, 'block_info');
    }
    if (isset($block_info[$block->module][$block->delta]['info'])) {
      $block->info = $block_info[$block->module][$block->delta]['info'];
      if ($exact_string && !empty($string) && $string == $block->info || !empty($string) && (!empty($block->info) && stripos($block->info, $string) !== FALSE || !empty($block->title) && stripos($block->title, $string) !== FALSE || !empty($block->module) && stripos($block->module . ' ' . $block->delta, $string) !== FALSE || !empty($block->bid) && stripos($block->bid, $string) !== FALSE) || empty($string)) {
        $rows[$block->bid] = $return_full_blocks ? $block : $block->info;
      }
    }
  }

  // Default sort.
  if ($return_full_blocks) {
    _blockreference_sort_blocks($rows);
  }
  else {
    natcasesort($rows);
  }
  return $rows;
}