Commit e0dda679 authored by acbramley's avatar acbramley

Inject block manager into the BlockFieldWidget class and tidy up the class.

parent 7d89db37
......@@ -3,11 +3,14 @@
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'block_field' widget.
......@@ -20,7 +23,40 @@ use Drupal\Core\Form\SubformState;
* }
* )
*/
class BlockFieldWidget extends WidgetBase {
class BlockFieldWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* The block manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
/**
* Set the block manager.
*
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager.
*/
public function setBlockManager(BlockManagerInterface $block_manager) {
$this->blockManager = $block_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings']
);
$instance->setBlockManager($container->get('plugin.manager.block'));
return $instance;
}
/**
* {@inheritdoc}
......@@ -40,7 +76,10 @@ class BlockFieldWidget extends WidgetBase {
$item =& $items[$delta];
$field_name = $this->fieldDefinition->getName();
$settings_id = implode('-', array_merge($element['#field_parents'], [$field_name, $delta, 'settings']));
$settings_id = implode('-', array_merge(
$element['#field_parents'],
[$field_name, $delta, 'settings']
));
$plugin_ids = $this->fieldDefinition->getSetting('plugin_ids');
......@@ -105,7 +144,6 @@ class BlockFieldWidget extends WidgetBase {
$element['settings']['admin_label']['#access'] = FALSE;
}
// DEBUG:
$element['#element_validate'] = [[$this, 'validate']];
}
......@@ -184,9 +222,6 @@ class BlockFieldWidget extends WidgetBase {
* {@inheritdoc}
*/
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// @todo: Inject the block manager as proper dependency.
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$field_name = $this->fieldDefinition->getName();
// Some blocks clean the processed values in form state. However, entity
......@@ -198,7 +233,7 @@ class BlockFieldWidget extends WidgetBase {
foreach ($values as $delta => &$value) {
// Execute block submit configuration in order to transform the form
// values into block configuration.
if (!empty($value['plugin_id']) && !empty($value['settings']) && $block = $block_manager->createInstance($value['plugin_id'])) {
if (!empty($value['plugin_id']) && !empty($value['settings']) && $block = $this->blockManager->createInstance($value['plugin_id'])) {
$elements = &$form[$field_name]['widget'][$delta]['settings'];
$subform_state = SubformState::createForSubform($elements, $form_state->getCompleteForm(), $form_state);
$block->submitConfigurationForm($elements, $subform_state);
......
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