For a view I needed an existing block in the header and footer and was quite surprised that Views didn't support that so I copied views_handler_area_view.inc and changed it so you can place an existing block in an area (footer, header, empty text, ?).
class views_handler_area_block extends views_handler_area {
function option_definition() {
$options = parent::option_definition();
$options['block_to_insert'] = array();
return $options;
}
/**
* Default options form that provides the label widget that all fields
* should have.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$query = db_select('block', 'b');
$result = $query
->fields('b')
->orderBy('b.module')
->addTag('block_load')
->addTag('translatable')
->execute();
$block_info = $result->fetchAllAssoc('bid');
// Allow modules to modify the block list.
drupal_alter('block_list', $block_info);
$blocks = array();
foreach ($block_info as $block) {
$blocks["{$block->module}:{$block->delta}"] = "{$block->module}:{$block->delta}"; // @TODO: fetch the correct block title
}
$form['block_to_insert'] = array(
'#type' => 'select',
'#title' => t('Block to insert'),
'#default_value' => $this->options['block_to_insert'],
'#description' => t('The block to insert into this area.'),
'#options' => $blocks,
);
}
/**
* Render the area
*/
function render($empty = FALSE) {
if ((!$empty || !empty($this->options['empty'])) && !empty($this->options['block_to_insert'])) {
list ($module, $delta) = explode(':', $this->options['block_to_insert'], 2);
$block = block_load($module, $delta);
if (empty($block)) {
return;
}
$block_content = _block_render_blocks(array($block));
$build = _block_get_renderable_array($block_content);
return drupal_render($build);
}
return '';
}
}
Save this as handlers/views_handler_area_view.inc and add it to the files array in views.info and add the following code to views_views_data() in modules/views.views.inc
if (module_exists('block')) {
$data['views']['block'] = array(
'title' => t('Block area'),
'help' => t('Insert a block inside an area.'),
'area' => array(
'handler' => 'views_handler_area_block',
),
);
}
Clear the cache and you then have a "Global: block" area to use in your views!
@TODO: fetch the correct block title
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | block_handler-1243948_6.patch | 3.96 KB | nils.destoop |
| #6 | block_handler-1243948_5.patch | 3.98 KB | nils.destoop |
| #3 | block_handler-1243948_2.patch | 3.4 KB | mrharolda |
| #2 | block_handler-1243948.patch | 3.36 KB | mrharolda |
Comments
Comment #1
dawehnerIn general it always help if you create a real patch file... it really helps.
There is a generic ctools content type issue #793814: Area handlers for CTools content types which allows you to embed basically everything,
also blocks and views.
Comment #2
mrharolda commentedSorry, I was in a bit of a hurry... ;)
Here's the patch against a fresh git checkout of views.
Comment #3
mrharolda commentedUpdated patch to exclude views blocks.
Comment #4
mrharolda commentedA new project also requires blocks to be embedded in view footers... Am I the only one with this request?
Comment #5
dawehnerPlease always set the right status :)
It would make more sense if you would a block.views.inc
Comment #6
nils.destoop commentedI also needed this for a project of mine. Included is the patch with block.views.inc.
Comment #7
mrharolda commented@zuuperman: bedankt! (Dutch for thanks!)
Marking as 'needs review'...
Comment #8
nils.destoop commentedNew patch with an updated file description from block.views.inc.
Comment #9
nils.destoop commentedComment #10
merlinofchaos commentedI think this might be best as a second module that people can add if they need this.
Comment #11
nils.destoop commentedSounds good. I will create a views_block_area module and create a project for it.
Comment #12
dawehnerCool. Let's mark this issue as fixed.
It would be cool if you would paste the link to the project once you have finished it.
Comment #13
nils.destoop commentedThe project can be found at http://drupal.org/project/views_block_area
Comment #14
mrharolda commented@zuuperman: Nice!
Too bad I didn't have/take the time myself. Thanks!!!
Comment #15.0
(not verified) commentedmerged separate function