Commit b55bc2c1 authored by fago's avatar fago Committed by jrockowitz

Issue #2843687 by fago: Block configuration forms are not submitted

parent e4a83df6
......@@ -7,12 +7,12 @@
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\block\BlockInterface;
use Drupal\Component\Utility\NestedArray;
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;
/**
* Plugin implementation of the 'block_field' widget.
......@@ -185,4 +185,32 @@ 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
// forms extract the form values twice during submission. For the second
// submission to work as well, we need to prevent the removal of the form
// values during the first submission.
$form_state = clone $form_state;
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'])) {
$elements = &$form[$field_name]['widget'][$delta]['settings'];
$subform_state = SubformState::createForSubform($elements, $form_state->getCompleteForm(), $form_state);
$block->submitConfigurationForm($elements, $subform_state);
$value['settings'] = $block->getConfiguration();
}
}
return $values;
}
}
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