You are here

function _blockreference_potential_references in Block reference 6

Same name and namespace in other branches
  1. 7 blockreference.module \_blockreference_potential_references()

Fetch an array of all candidate referenced blocks, for use in presenting the selection form to the user.

Parameters

$field: Array containing field data.

$return_full_blocks: Whether to return full blocks.

$string: Filter string to match blocks.

$exact_string: Strictly match string like for validation.

4 calls to _blockreference_potential_references()
blockreference_allowed_values in ./blockreference.module
Implementation of hook_allowed_values().
blockreference_autocomplete in ./blockreference.module
Retrieve a pipe delimited string of autocomplete suggestions
blockreference_autocomplete_validate in ./blockreference.module
Validate an autocomplete element.
blockreference_field in ./blockreference.module
Implementation of hook_field().

File

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

Code

function _blockreference_potential_references($field, $return_full_blocks = FALSE, $string = '', $exact_string = FALSE) {
  static $block_info = array();
  $related_regions = array();
  $related_modules = array();
  $related_clauses = array();
  $args = array();

  // Handle related regions - this will be used in extra query conditions.
  if (isset($field['referenceable_regions']) && is_array($field['referenceable_regions'])) {
    foreach ($field['referenceable_regions'] as $related_region) {
      if ($related_region !== 0 && $related_region != '0') {
        if (isset($related_region)) {
          $related_regions[] = " b.region = '%s'";
          $args[] = $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($field['referenceable_modules']) && is_array($field['referenceable_modules'])) {
    foreach ($field['referenceable_modules'] as $related_module) {
      if ($related_module !== 0 && $related_module != '0') {
        $related_modules[] = " b.module = '%s'";
        $args[] = $related_module;
      }
    }
  }
  if (!empty($related_modules)) {
    $related_clauses[] = implode(' OR ', $related_modules);
  }

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

  // Assemble the query.
  $result = db_query('SELECT b.bid, b.module, b.delta, b.title, b.region ' . 'FROM {blocks} b ' . "WHERE b.theme = '" . variable_get('theme_default', 'garland') . "'" . $related_clause . ' ' . 'ORDER BY b.region, b.weight', $args);

  // Execute the query, test each row, and return the blocks or block info.
  $rows = array();
  while ($block = db_fetch_object($result)) {
    if (!isset($block_info[$block->module][$block->delta])) {
      $block_info[$block->module] = module_invoke($block->module, 'block', 'list');
    }
    $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;
    }
  }
  drupal_alter('blockreference_potential_references', $rows, $field, $return_full_blocks, $string, $exact_string);
  return $rows;
}