function views_block

Implementation of hook_block

File

./views.module, line 454

Code

function views_block($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
        case 'list':
            // Try to avoid instantiating all the views just to get the blocks info.
            views_include('cache');
            $cache = views_cache_get('views_block_items', TRUE);
            if ($cache && is_array($cache->data)) {
                return $cache->data;
            }
            $items = array();
            $views = views_get_all_views();
            foreach ($views as $view) {
                // disabled views get nothing.
                if (!empty($view->disabled)) {
                    continue;
                }
                $view->init_display();
                foreach ($view->display as $display_id => $display) {
                    if (isset($display->handler) && !empty($display->handler->definition['uses hook block'])) {
                        $result = $display->handler
                            ->execute_hook_block();
                        if (is_array($result)) {
                            $items = array_merge($items, $result);
                        }
                    }
                    if (isset($display->handler) && $display->handler
                        ->get_option('exposed_block')) {
                        $result = $display->handler
                            ->get_special_blocks();
                        if (is_array($result)) {
                            $items = array_merge($items, $result);
                        }
                    }
                }
            }
            // block.module has a delta length limit of 32, but our deltas can
            // unfortunately be longer because view names can be 32 and display IDs
            // can also be 32. So for very long deltas, change to md5 hashes.
            $hashes = array();
            // get the keys because we're modifying the array and we don't want to
            // confuse PHP too much.
            $keys = array_keys($items);
            foreach ($keys as $delta) {
                if (strlen($delta) >= 32) {
                    $hash = md5($delta);
                    $hashes[$hash] = $delta;
                    $items[$hash] = $items[$delta];
                    unset($items[$delta]);
                }
            }
            // Only save hashes if they have changed.
            $old_hashes = variable_get('views_block_hashes', array());
            if ($hashes != $old_hashes) {
                variable_set('views_block_hashes', $hashes);
            }
            // Save memory: Destroy those views.
            foreach ($views as $view) {
                $view->destroy();
            }
            views_cache_set('views_block_items', $items, TRUE);
            return $items;
        case 'view':
            // if this is 32, this should be an md5 hash.
            if (strlen($delta) == 32) {
                $hashes = variable_get('views_block_hashes', array());
                if (!empty($hashes[$delta])) {
                    $delta = $hashes[$delta];
                }
            }
            // This indicates it's a special one.
            if (substr($delta, 0, 1) == '-') {
                list($nothing, $type, $name, $display_id) = explode('-', $delta);
                // Put the - back on.
                $type = '-' . $type;
                if ($view = views_get_view($name)) {
                    if ($view->access($display_id)) {
                        $view->set_display($display_id);
                        if (isset($view->display_handler)) {
                            $output = $view->display_handler
                                ->view_special_blocks($type);
                            $view->destroy();
                            return $output;
                        }
                    }
                    $view->destroy();
                }
            }
            list($name, $display_id) = explode('-', $delta);
            // Load the view
            if ($view = views_get_view($name)) {
                if ($view->access($display_id)) {
                    $output = $view->execute_display($display_id);
                    $view->destroy();
                    return $output;
                }
                $view->destroy();
            }
            break;
    }
}