You are here

function _blockreference_find_blocks in Block reference 7.2

Helper to find blocks that potentially match a search string, in the right order, altered by custom modules.

3 calls to _blockreference_find_blocks()
blockreference_autocomplete in ./blockreference.module
Menu callback for the autocomplete results.
blockreference_autocomplete_validate in ./blockreference.module
Validation callback for a blockreference autocomplete element.
blockreference_options_list in ./blockreference.module
Implements hook_options_list().

File

./blockreference.module, line 333

Code

function _blockreference_find_blocks($instance, $search_string = '', $context = array()) {
  $all_block_infos =& drupal_static(__FUNCTION__, array());
  $iid = $instance['id'];

  // Cache ALL potential block objects for this instance.
  if (!isset($all_block_infos[$iid])) {
    $referenceable_modules = _blockreference_get_block_modules_from_field($instance);
    $all_block_infos[$iid] = array();
    foreach (module_implements('block_info') as $module) {
      if (!$referenceable_modules || isset($referenceable_modules[$module])) {
        if ($blocks = module_invoke($module, 'block_info')) {
          foreach ($blocks as $delta => $info) {
            $moddelta = $module . ':' . $delta;
            $all_block_infos[$iid][$moddelta] = _blockreference_block($module, $delta, $info);
          }
        }
      }
    }
  }
  $blocks = $all_block_infos[$iid];
  $context['instance'] = $instance;
  drupal_alter('blockreference_blocks_pre', $blocks, $context);

  // Filter by search string.
  if ($search_string) {
    $check_string = drupal_strtolower($search_string);
    foreach ($blocks as $moddelta => $block) {
      if (strpos(drupal_strtolower($block->info), $check_string) === FALSE) {
        unset($blocks[$moddelta]);
      }
    }
  }

  // Sort.
  uasort($blocks, function ($a, $b) {
    return strnatcasecmp($a->info, $b->info);
  });
  drupal_alter('blockreference_blocks_post', $blocks, $context);
  return $blocks;
}