Commit e72136c7 authored by jrockowitz's avatar jrockowitz

Fix issue with label_display, add block validate configuration handling, and...

Fix issue with label_display, add block validate configuration handling, and change field data from configuration to settings.
parent 0cfe6d8e
......@@ -28,11 +28,11 @@ class BlockFieldFormatter extends FormatterBase {
$elements = array();
foreach ($items as $delta => $item) {
$plugin_id = $item->id;
$configuration = $item->configuration;
$settings = $item->settings;
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$block_instance = $block_manager->createInstance($plugin_id, $configuration);
$block_instance = $block_manager->createInstance($plugin_id, $settings);
$elements[$delta] = [
'#theme' => 'block',
......
......@@ -29,7 +29,7 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
public static function defaultFieldSettings() {
return array(
'id' => '',
'configuration' => [],
'settings' => [],
) + parent::defaultFieldSettings();
}
......@@ -48,8 +48,8 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
->setLabel(t('ID'))
->setRequired(TRUE);
$properties['configuration'] = MapDataDefinition::create()
->setLabel(t('Configuration'));
$properties['settings'] = MapDataDefinition::create()
->setLabel(t('Settings'));
return $properties;
}
......@@ -65,8 +65,8 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
'type' => 'varchar',
'length' => 255,
),
'configuration' => array(
'description' => 'Serialized array of configuration for the block.',
'settings' => array(
'description' => 'Serialized array of settings for the block.',
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
......@@ -94,12 +94,12 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
}
if (isset($values)) {
$values += [
'configuration' => [],
'settings' => [],
];
}
// Unserialize the values.
if (is_string($values['configuration'])) {
$values['configuration'] = unserialize($values['configuration']);
if (is_string($values['settings'])) {
$values['settings'] = unserialize($values['settings']);
}
parent::setValue($values, $notify);
}
......
......@@ -6,6 +6,7 @@ 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;
/**
......@@ -27,7 +28,7 @@ class BlockFieldWidget extends WidgetBase {
public static function defaultSettings() {
return array(
'id' => '',
'configuration' => [],
'settings' => [],
) + parent::defaultSettings();
}
......@@ -42,11 +43,11 @@ class BlockFieldWidget extends WidgetBase {
$item = $items[$delta];
$field_name = $this->fieldDefinition->getName();
$configuration_id = $field_name . '-' . $delta . '-configuration';
$configuration_id = $field_name . '-' . $delta . '-settings';
$values = $form_state->getValues();
$plugin_id = (isset($values[$field_name][$delta]['id'])) ? $values[$field_name][$delta]['id'] : $item->id;
$configuration = (!empty($values[$field_name][$delta]['configuration'])) ? $values[$field_name][$delta]['configuration'] : $item->configuration ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$settings = (!empty($values[$field_name][$delta]['settings'])) ? $values[$field_name][$delta]['settings'] : $item->settings ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
......@@ -71,7 +72,7 @@ class BlockFieldWidget extends WidgetBase {
$block_manager = \Drupal::service('plugin.manager.block');
// Build configuration container.
$element['configuration'] = [
$element['settings'] = [
'#type' => 'container',
'#attributes' => ['id' => $configuration_id],
'#tree' => TRUE,
......@@ -79,16 +80,16 @@ 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, $configuration);
$element['configuration'] += $block_instance->buildConfigurationForm([], $form_state);
$block_instance = $block_manager->createInstance($plugin_id, $settings);
$element['settings'] += $block_instance->buildConfigurationForm([], $form_state);
// Hide admin label (aka description).
if (isset($element['configuration']['admin_label'])) {
$element['configuration']['admin_label']['#access'] = FALSE;
if (isset($element['settings']['admin_label'])) {
$element['settings']['admin_label']['#access'] = FALSE;
}
// DEBUG:
// $element['configuration']['#element_validate'] = array(array($this, 'validateConfiguration'));
$element['#element_validate'] = array(array($this, 'validate'));
}
return $element;
......@@ -97,16 +98,35 @@ class BlockFieldWidget extends WidgetBase {
public function configurationForm(array $form, FormStateInterface $form_state) {
$trigger_element = $form_state->getTriggeringElement();
$array_parents = $trigger_element['#array_parents'];
$array_parents[(count($array_parents) - 1)] = 'configuration';
$array_parents[(count($array_parents) - 1)] = 'settings';
return NestedArray::getValue($form, $array_parents);
}
/**
* Form element validation handler for block configuration.
* Form element validation handler.
*/
public static function validateConfiguration($element, FormStateInterface $form_state, $form) {
public static function validate($element, FormStateInterface $form_state, $form) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$values = $form_state->getValues();
$array_parents = $element['#parents'];
$configuration = NestedArray::getValue($values, $array_parents);
$plugin_id = NestedArray::getValue($values, $element['id']['#parents']);
$settings = NestedArray::getValue($values, $element['settings']['#parents']);
// Convert label display to FALSE instead of 0. This allow the label to be
// hidden.
if ($settings['label_display'] === 0) {
$settings['label_display'] = FALSE;
}
// Execute block validate configuration.
if (!empty($plugin_id) && $block_manager->hasDefinition($plugin_id)) {
$block_instance = $block_manager->createInstance($plugin_id, $settings);
$settings = (new FormState())->setValues($settings);
$block_instance->validateConfigurationForm($form, $settings);
}
NestedArray::setValue($values, $element['settings']['#parents'], $settings->getValues());
$form_state->setValues($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