Skip to content

Commit

Permalink
Issue #2888733: Drupal 8 version
Browse files Browse the repository at this point in the history
  • Loading branch information
leonk committed Jun 23, 2017
0 parents commit 56698a6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/Plugin/views/area/ViewsBlockArea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/**
* @file
* Contains \Drupal\block\Plugin\views\area\Block.
*/

namespace Drupal\views_block_area\Plugin\views\area;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\area\AreaPluginBase;

/**
* Provides an area handler which renders a block entity in a certain view mode.
*
* @ingroup views_area_handlers
*
* @ViewsArea("views_block_area")
*/
class ViewsBlockArea extends AreaPluginBase {

/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();

$options['block_id'] = ['default' => NULL];

return $options;
}

/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);

$options = [];
/** @var \Drupal\block_field\BlockFieldManagerInterface $block_field_manager */
$block_field_manager = \Drupal::service('block_field.manager');
$definitions = $block_field_manager->getBlockDefinitions();
foreach ($definitions as $id => $definition) {
// If allowed plugin ids are set then check that this block should be
// included.
$category = (string) $definition['category'];
$options[$category][$id] = $definition['admin_label'];
}

$form['block_id'] = [
'#type' => 'select',
'#title' => $this->t('Block'),
'#options' => $options,
'#empty_option' => $this->t('Please select'),
'#default_value' => $this->options['block_id'],
];
}

/**
* {@inheritdoc}
*/
public function render($empty = FALSE) {
$element = [];
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$block_instance = $this->getBlock();
// Make sure the block exists and is accessible.
if (!$block_instance || !$block_instance->access(\Drupal::currentUser())) {
return NULL;
}

// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$element = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block_instance->getConfiguration(),
'#plugin_id' => $block_instance->getPluginId(),
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $block_instance->getPluginId(),
'content' => $block_instance->build(),
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$renderer->addCacheableDependency($element, $block_instance);
return $element;
}

/**
* {@inheritdoc}
*/
protected function getBlock() {
if (empty($this->options['block_id'])) {
return NULL;
}

/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');

/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
$block_instance = $block_manager->createInstance($this->options['block_id'], []);

$plugin_definition = $block_instance->getPluginDefinition();

// Don't return broken block plugin instances.
if ($plugin_definition['id'] == 'broken') {
return NULL;
}

// Don't return broken block content instances.
if ($plugin_definition['id'] == 'block_content') {
$uuid = $block_instance->getDerivativeId();
if (!\Drupal::entityManager()->loadEntityByUuid('block_content', $uuid)) {
return NULL;
}
}

return $block_instance;
}
}
7 changes: 7 additions & 0 deletions views_block_area.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: Views block area
type: module
description: Exposes all blocks as a views area.
core: 8.x
package: Views
dependencies:
- views
23 changes: 23 additions & 0 deletions views_block_area.views.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<<?php

/**
* @file
* Provide views data for views_block_area module.
*/

/**
* Implements hook_views_data().
*/
function views_block_area_views_data() {
$data = [];

$data['views']['views_block_area'] = [
'title' => t('Block area'),
'help' => t('Insert a block inside an area.'),
'area' => [
'id' => 'views_block_area',
],
];

return $data;
}

0 comments on commit 56698a6

Please sign in to comment.