Commit 67388379 authored by jrockowitz's avatar jrockowitz

Cleaning up API.

parent 86daee4e
......@@ -7,6 +7,7 @@
namespace Drupal\block_field\Plugin\Field\FieldFormatter;
use Drupal\block_field\Plugin\Field\FieldType\BlockFieldItem;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
......@@ -32,23 +33,20 @@ class BlockFieldFormatter extends FormatterBase {
$elements = [];
foreach ($items as $delta => $item) {
$plugin_id = $item->id;
$settings = $item->settings;
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$block_instance = $block_manager->createInstance($plugin_id, $settings);
$elements[$delta] = [
'#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' => $item->id,
'content' => $block_instance->build(),
];
if ($block_instance = $item->getBlock()) {
$elements[$delta] = [
'#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' => $item->id,
'content' => $block_instance->build(),
];
}
}
return $elements;
......
......@@ -150,6 +150,39 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
parent::setValue($values, $notify);
}
/**
* Get block instance
*
* @return null|\Drupal\Core\Block\BlockPluginInterface
*/
public function getBlock() {
if (empty($this->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->id, $this->settings);
$plugin_definition = $block_instance->getPluginDefinition();
// Don't render or return broken block instances.
if ($plugin_definition['id'] == 'broken') {
return NULL;
}
// Make sure block content exists, if not don't render the broken block
// message.
if ($plugin_definition['id'] == 'block_content') {
$uuid = $block_instance->getDerivativeId();
if (!\Drupal::entityManager()->loadEntityByUuid('block_content', $uuid)) {
return NULL;
}
}
return $block_instance;
}
/**
* Validates plugin table select element.
*/
......
......@@ -8,6 +8,7 @@
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\block\BlockInterface;
use Drupal\block_field\Plugin\Field\FieldType\BlockFieldItem;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
......@@ -53,8 +54,14 @@ class BlockFieldWidget extends WidgetBase {
$plugin_ids = $this->fieldDefinition->getSetting('plugin_ids');
$values = $form_state->getValues();
$plugin_id = (isset($values[$field_name][$delta]['id'])) ? $values[$field_name][$delta]['id'] : $item->id;
$settings = (!empty($values[$field_name][$delta]['settings'])) ? $values[$field_name][$delta]['settings'] : $item->settings ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$item->id = (isset($values[$field_name][$delta]['id'])) ? $values[$field_name][$delta]['id'] : $item->id;
if (!empty($values[$field_name][$delta]['settings'])) {
$item->settings = $values[$field_name][$delta]['settings'];
}
else {
$item->settings = $item->settings ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
}
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
......@@ -64,7 +71,7 @@ class BlockFieldWidget extends WidgetBase {
continue;
}
$category = (string) $definition['category'];
$options[$category][$id] = $definition['admin_label'] . ' (' . $id . ')';
$options[$category][$id] = $definition['admin_label'];
}
$element['id'] = [
......@@ -72,7 +79,7 @@ class BlockFieldWidget extends WidgetBase {
'#title' => $this->t('ID'),
'#options' => $options,
'#empty_option' => $this->t('- None -'),
'#default_value' => $plugin_id,
'#default_value' => $item->id,
'#required' => $element['#required'],
'#ajax' => [
'callback' => [$this, 'configurationForm'],
......@@ -80,9 +87,6 @@ class BlockFieldWidget extends WidgetBase {
],
];
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
// Build configuration container.
$element['settings'] = [
'#type' => 'container',
......@@ -91,8 +95,7 @@ class BlockFieldWidget extends WidgetBase {
];
// If block plugin exists get the block's configuration form.
if (!empty($plugin_id) && $block_manager->hasDefinition($plugin_id)) {
$block_instance = $block_manager->createInstance($plugin_id, $settings);
if ($block_instance = $item->getBlock()) {
$element['settings'] += $block_instance->buildConfigurationForm([], $form_state);
// Hide admin label (aka description).
......
......@@ -14,7 +14,7 @@ use Drupal\Core\Form\FormStateInterface;
* Provides a 'Block field test' block.
*
* @Block(
* id = "block_field_test",
* id = "block_field_testBROKEN",
* admin_label = @Translation("Block field test"),
* category = @Translation("Block field test")
* )
......
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