Commit 04c3129a authored by jrockowitz's avatar jrockowitz

Fix label not changing when toggling between block plugins.

parent 67388379
......@@ -28,9 +28,6 @@ class BlockFieldFormatter extends FormatterBase {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$elements = [];
foreach ($items as $delta => $item) {
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
......
......@@ -98,12 +98,16 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
}
$default_value = $field->getSetting('plugin_ids') ?: array_keys($options);
$element = [];
$element['plugin_ids'] = [
'#type' => 'tableselect',
$element = [];
$element['blocks'] = [
'#type' => 'details',
'#title' => $this->t('Blocks'),
'#title_display' => 'invisible',
'#description' => $this->t('Please select available blocks.'),
'#open' => $field->getSetting('plugin_ids') ? TRUE : FALSE,
];
$element['blocks']['plugin_ids'] = [
'#type' => 'tableselect',
'#header' => [
'Category',
'Label/ID',
......@@ -113,10 +117,10 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
'#js_select' => TRUE,
'#required' => TRUE,
'#empty' => t('No blocks are available.'),
'#parents' => ['settings', 'plugin_ids'],
'#element_validate' => [[get_called_class(), 'validatePluginIds']],
'#default_value' => array_combine($default_value, $default_value),
];
return $element;
}
......@@ -184,7 +188,7 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
}
/**
* Validates plugin table select element.
* Validates plugin_ids table select element.
*/
public static function validatePluginIds(array &$element, FormStateInterface $form_state, &$complete_form) {
$value = array_filter($element['#value']);
......
......@@ -117,7 +117,18 @@ class BlockFieldWidget extends WidgetBase {
$trigger_element = $form_state->getTriggeringElement();
$array_parents = $trigger_element['#array_parents'];
$array_parents[(count($array_parents) - 1)] = 'settings';
return NestedArray::getValue($form, $array_parents);
$settings_element = NestedArray::getValue($form, $array_parents);
// Set the label #value to the default block instance's label.
$plugin_id = $trigger_element['#value'];
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
/** @var \Drupal\Core\Block\BlockPluginInterface $block_instance */
if ($block_instance = $block_manager->createInstance($plugin_id)) {
$settings_element['label']['#value'] = $block_instance->label();
}
return $settings_element;
}
/**
......
......@@ -14,7 +14,7 @@ use Drupal\Core\Form\FormStateInterface;
* Provides a 'Block field test' block.
*
* @Block(
* id = "block_field_testBROKEN",
* id = "block_field_test",
* 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