Commit 3dc43bd8 authored by fenstrat's avatar fenstrat Committed by fenstrat

Issue #2877520 by fenstrat, acbramley, finne: Add form display option to...

Issue #2877520 by fenstrat, acbramley, finne: Add form display option to control display of block configuration form
parent b1d5f822
......@@ -17,6 +17,8 @@ field.widget.settings.block_field_default:
label: 'Settings'
sequence:
type: string
configuration_form:
type: string
field.storage_settings.block_field:
type: mapping
......
......@@ -65,9 +65,37 @@ class BlockFieldWidget extends WidgetBase implements ContainerFactoryPluginInter
return [
'plugin_id' => '',
'settings' => [],
'configuration_form' => 'full',
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['configuration_form'] = [
'#type' => 'select',
'#title' => $this->t('Configuration form'),
'#description' => $this->t('How the block configuration form will be shown.'),
'#options' => [
'full' => $this->t('Full'),
'hidden' => $this->t('Hidden'),
],
'#default_value' => $this->getSetting('configuration_form'),
'#required' => TRUE,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary[] = $this->t('Configuration form: @configuration_form', ['@configuration_form' => $this->getSetting('configuration_form')]);
return $summary;
}
/**
* {@inheritdoc}
*/
......@@ -122,29 +150,33 @@ class BlockFieldWidget extends WidgetBase implements ContainerFactoryPluginInter
'#empty_option' => $this->t('- None -'),
'#default_value' => $item->plugin_id,
'#required' => $element['#required'],
'#ajax' => [
];
// Show configuration form if required.
if ($this->getSetting('configuration_form') === 'full') {
$element['plugin_id']['#ajax'] = [
'callback' => [$this, 'configurationForm'],
'wrapper' => $settings_id,
],
];
];
// Build configuration container.
$element['settings'] = [
'#type' => 'container',
'#attributes' => ['id' => $settings_id],
'#tree' => TRUE,
];
// Build configuration container.
$element['settings'] = [
'#type' => 'container',
'#attributes' => ['id' => $settings_id],
'#tree' => TRUE,
];
// If block plugin exists get the block's configuration form.
if ($block_instance = $item->getBlock()) {
$element['settings'] += $block_instance->buildConfigurationForm([], $form_state);
// If block plugin exists get the block's configuration form.
if ($block_instance = $item->getBlock()) {
$element['settings'] += $block_instance->buildConfigurationForm([], $form_state);
// Hide admin label (aka description).
if (isset($element['settings']['admin_label'])) {
$element['settings']['admin_label']['#access'] = FALSE;
}
// Hide admin label (aka description).
if (isset($element['settings']['admin_label'])) {
$element['settings']['admin_label']['#access'] = FALSE;
}
$element['#element_validate'] = [[$this, 'validate']];
$element['#element_validate'] = [[$this, 'validate']];
}
}
return $element;
......
......@@ -26,6 +26,13 @@ class WidgetTest extends BrowserTestBase {
'field_ui',
];
/**
* The test block node.
*
* @var \Drupal\node\NodeInterface
*/
protected $blockNode;
/**
* {@inheritdoc}
*/
......@@ -33,12 +40,21 @@ class WidgetTest extends BrowserTestBase {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser([
'access administration pages',
'access content',
'administer nodes',
'administer content types',
'bypass node access',
'administer node fields',
'administer node form display',
'administer nodes',
'bypass node access',
]));
$this->drupalPostForm('node/add/block_node', [
'title[0][value]' => 'Block field test',
'field_block[0][plugin_id]' => 'views_block:items-block_1',
], $this->t('Save'));
$this->blockNode = $this->drupalGetNodeByTitle('Block field test');
}
/**
......@@ -47,13 +63,7 @@ class WidgetTest extends BrowserTestBase {
public function testBlockSettingsAreStoredCorrectly() {
$items = $this->createDummyNodes('item', 5);
$this->drupalPostForm('node/add/block_node', [
'title[0][value]' => 'Block field test',
'field_block[0][plugin_id]' => 'views_block:items-block_1',
], $this->t('Save'));
$node = $this->drupalGetNodeByTitle('Block field test');
$this->drupalGet($node->toUrl('edit-form'));
$this->drupalGet($this->blockNode->toUrl('edit-form'));
$this->submitForm([
'field_block[0][settings][override][items_per_page]' => 5,
], $this->t('Save'));
......@@ -63,6 +73,33 @@ class WidgetTest extends BrowserTestBase {
}
}
/**
* Test configuration form options.
*/
public function testConfigurationFormOptions() {
$assert = $this->assertSession();
// Configuration form: full (the default).
$this->drupalGet($this->blockNode->toUrl('edit-form'));
$assert->fieldExists('field_block[0][settings][label_display]');
$assert->fieldExists('field_block[0][settings][override][items_per_page]');
$assert->fieldExists('field_block[0][settings][views_label_checkbox]');
$assert->fieldExists('field_block[0][settings][views_label]');
// Configuration form: hidden.
$this->drupalGet('admin/structure/types/manage/block_node/form-display');
$this->drupalPostForm(NULL, [], 'field_block_settings_edit');
$edit = [
'fields[field_block][settings_edit_form][settings][configuration_form]' => 'hidden',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalGet($this->blockNode->toUrl('edit-form'));
$assert->fieldNotExists('field_block[0][settings][label_display]');
$assert->fieldNotExists('field_block[0][settings][override][items_per_page]');
$assert->fieldNotExists('field_block[0][settings][views_label_checkbox]');
$assert->fieldNotExists('field_block[0][settings][views_label]');
}
/**
* Create dummy nodes.
*
......
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