diff --git a/config/schema/block_field.yml b/config/schema/block_field.yml index 7ba7fe7..be85208 100644 --- a/config/schema/block_field.yml +++ b/config/schema/block_field.yml @@ -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 diff --git a/src/Plugin/Field/FieldWidget/BlockFieldWidget.php b/src/Plugin/Field/FieldWidget/BlockFieldWidget.php index 03d329d..2e63012 100644 --- a/src/Plugin/Field/FieldWidget/BlockFieldWidget.php +++ b/src/Plugin/Field/FieldWidget/BlockFieldWidget.php @@ -29,12 +29,40 @@ class BlockFieldWidget extends WidgetBase { return [ 'plugin_id' => '', 'settings' => [], + 'configuration_form' => 'full', ] + parent::defaultSettings(); } /** * {@inheritdoc} */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $elements['configuration_form'] = array( + '#type' => 'select', + '#title' => $this->t('Configuration form'), + '#description' => $this->t('How the block configuration form will be shown.'), + '#options' => array( + '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} + */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\block_field\BlockFieldItemInterface $item */ $item =& $items[$delta]; @@ -83,30 +111,34 @@ class BlockFieldWidget extends WidgetBase { '#empty_option' => $this->t('- None -'), '#default_value' => $item->plugin_id, '#required' => $element['#required'], - '#ajax' => [ - 'callback' => [$this, 'configurationForm'], - 'wrapper' => $settings_id, - ], - ]; - - // 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); - - // Hide admin label (aka description). - if (isset($element['settings']['admin_label'])) { - $element['settings']['admin_label']['#access'] = FALSE; + // 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, + ]; + + // 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; + } + + // DEBUG: + $element['#element_validate'] = [[$this, 'validate']]; } - - // DEBUG: - $element['#element_validate'] = [[$this, 'validate']]; } return $element; diff --git a/tests/src/Functional/WidgetTest.php b/tests/src/Functional/WidgetTest.php index 405e14a..1d62f2b 100644 --- a/tests/src/Functional/WidgetTest.php +++ b/tests/src/Functional/WidgetTest.php @@ -40,6 +40,7 @@ class WidgetTest extends BrowserTestBase { * @test */ public function blockSettingsAreStoredCorrectly() { + // Configuration form: full. $nodes = $this->createDummyNodes('item', 5); $this->drupalPostForm('node/add/block_node', [ 'title[0][value]' => 'Block field test', @@ -51,10 +52,28 @@ class WidgetTest extends BrowserTestBase { 'field_block[0][settings][override][items_per_page]' => 5, ], $this->t('Save and keep published')); + // Overridden value of 5 nodes should show. + $test_nodes = $nodes; do { - $this->assertSession()->pageTextContains(array_pop($nodes)->getTitle()); + $this->assertSession()->pageTextContains(array_pop($test_nodes)->getTitle()); } - while (count($nodes)); + while (count($test_nodes)); + + // Configuration form: hidden. + $this->drupalPostAjaxForm('admin/structure/types/manage/block_node/form-display', [], 'field_block_settings_edit'); + $this->drupalPostForm(NULL, ['fields[field_block][settings_edit_form][settings][configuration_form]' => 'hidden'], t('Update')); + $this->drupalPostForm(NULL, [], t('Save')); + + $this->drupalGet('node/6/edit'); + $this->submitForm([], $this->t('Save and keep published')); + + // Default view items_per_page value of 1 node should show. + $test_nodes = $nodes; + do { + $contains = count($test_nodes) == 5 ? 'pageTextContains' : 'pageTextNotContains'; + $this->assertSession()->{$contains}(array_pop($test_nodes)->getTitle()); + } + while (count($test_nodes)); } /**