Commit 45da6270 authored by jrockowitz's avatar jrockowitz

Create BlockFieldManager and ignore context aware blocks.

parent 0f2242e6
......@@ -7,6 +7,7 @@ Contents of this file
* Demo
* Installation
* Notes
* Known Issues/Limitations
* Todo
......@@ -52,6 +53,14 @@ Notes
- All content blocks from the 'Custom Block Library' are available.
Known Issues/Limitations
------------------------
- Context aware plugins are not currently supported. This includes ctools
entity view blocks.
See: \Drupal\block_field\BlockFieldManager::getBlockDefinitions
Todo
----
......
services:
block_field.manager:
class: Drupal\block_field\BlockFieldManager
arguments: ['@plugin.manager.block']
<?php
/**
* @file
* Contains \Drupal\block_field\BlockFieldManager.
*/
namespace Drupal\block_field;
use \Drupal\Core\Block\BlockManagerInterface;
/**
* Defines a service that manages block plugins for the block field.
*/
class BlockFieldManager implements BlockFieldManagerInterface {
/**
* The block plugin manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* Constructs a new BlockFieldManager.
*
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block plugin manager.
*/
public function __construct(BlockManagerInterface $block_manager) {
$this->blockManager = $block_manager;
}
/**
* {@inheritdoc}
*/
public function getBlockDefinitions() {
$definitions = $this->blockManager->getSortedDefinitions();
$block_definitions = [];
foreach ($definitions as $plugin_id => $definition) {
// Context aware plugins are not currently supported.
// Core and component plugins can be context-aware
// https://www.drupal.org/node/1938688
// @see \Drupal\ctools\Plugin\Block\EntityView
if (isset($definition['context'])) {
continue;
}
$block_definitions[$plugin_id] = $definition;
}
return $block_definitions;
}
}
<?php
/**
* @file
* Contains \Drupal\block_field\BlockFieldManagerInterface.
*/
namespace Drupal\block_field;
/**
* Provides an interface defining a BLock field manager.
*/
interface BlockFieldManagerInterface {
/**
* Get sorted listed of supported block definitions.
*
* @return array
* An associative array of supported block definitions.
*/
public function getBlockDefinitions();
}
......@@ -29,12 +29,20 @@ class BlockFieldFormatter extends FormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$block_instance = $item->getBlock();
// Make sure the block exists and is accessible.
if (!$block_instance || !$block_instance->access(\Drupal::currentUser())) {
continue;
}
try {
$build = $block_instance->build();
}
catch (\Exception $exception) {
continue;
}
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$elements[$delta] = [
......@@ -45,7 +53,7 @@ class BlockFieldFormatter extends FormatterBase {
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $item->plugin_id,
'content' => $block_instance->build(),
'content' => $build,
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
......
......@@ -86,10 +86,9 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$options = [];
$definitions = $block_manager->getSortedDefinitions();
/** @var \Drupal\block_field\BlockFieldManagerInterface $block_field_manager */
$block_field_manager = \Drupal::service('block_field.manager');
$definitions = $block_field_manager->getBlockDefinitions();
foreach ($definitions as $plugin_id => $definition) {
$options[$plugin_id] = [
['category' => (string) $definition['category']],
......
......@@ -45,7 +45,7 @@ class BlockFieldWidget extends WidgetBase {
$block_manager = \Drupal::service('plugin.manager.block');
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$item = $items[$delta];
$item =& $items[$delta];
$field_name = $this->fieldDefinition->getName();
$settings_id = $field_name . '-' . $delta . '-settings';
......@@ -58,21 +58,32 @@ class BlockFieldWidget extends WidgetBase {
$item->settings = $values[$field_name][$delta]['settings'];
}
else {
$item->settings = $item->settings ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$item->settings = $item->settings ?: [];
}
$options = [];
$definitions = $block_manager->getSortedDefinitions();
/** @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.
if ($plugin_ids && !isset($plugin_ids[$id])) {
// Remove the definition, so that we have an accurate list of allowed
// blocks definitions.
unset($definitions[$id]);
continue;
}
$category = (string) $definition['category'];
$options[$category][$id] = $definition['admin_label'];
}
// Make sure the plugin id is allowed, if not clear all settings.
if ($item->plugin_id && !isset($definitions[$item->plugin_id])) {
$item->plugin_id = '';
$item->setting = [];
}
$element['plugin_id'] = [
'#type' => 'select',
'#title' => $this->t('ID'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment