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

Comments

dawehner’s picture

In 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.

mrharolda’s picture

StatusFileSize
new3.36 KB

Sorry, I was in a bit of a hurry... ;)

Here's the patch against a fresh git checkout of views.

mrharolda’s picture

StatusFileSize
new3.4 KB

Updated patch to exclude views blocks.

mrharolda’s picture

A new project also requires blocks to be embedded in view footers... Am I the only one with this request?

dawehner’s picture

Status: Active » Needs work

Please always set the right status :)

+++ b/modules/views.views.incundefined
@@ -67,6 +67,16 @@ function views_views_data() {
+  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',
+      ),
+    );

It would make more sense if you would a block.views.inc

nils.destoop’s picture

StatusFileSize
new3.98 KB

I also needed this for a project of mine. Included is the patch with block.views.inc.

mrharolda’s picture

Status: Needs work » Needs review

@zuuperman: bedankt! (Dutch for thanks!)

Marking as 'needs review'...

nils.destoop’s picture

Status: Needs review » Needs work
StatusFileSize
new3.96 KB

New patch with an updated file description from block.views.inc.

nils.destoop’s picture

Status: Needs work » Needs review
merlinofchaos’s picture

I think this might be best as a second module that people can add if they need this.

nils.destoop’s picture

Sounds good. I will create a views_block_area module and create a project for it.

dawehner’s picture

Status: Needs review » Fixed

Cool. 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.

nils.destoop’s picture

The project can be found at http://drupal.org/project/views_block_area

mrharolda’s picture

@zuuperman: Nice!

Too bad I didn't have/take the time myself. Thanks!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

merged separate function