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 cc5958a..43cd98f 100644 --- a/src/Plugin/Field/FieldWidget/BlockFieldWidget.php +++ b/src/Plugin/Field/FieldWidget/BlockFieldWidget.php @@ -65,12 +65,40 @@ 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} + */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\block_field\BlockFieldItemInterface $item */ $item =& $items[$delta]; @@ -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; diff --git a/tests/src/Functional/WidgetTest.php b/tests/src/Functional/WidgetTest.php index 605a273..9dfc832 100644 --- a/tests/src/Functional/WidgetTest.php +++ b/tests/src/Functional/WidgetTest.php @@ -27,18 +27,34 @@ class WidgetTest extends BrowserTestBase { ]; /** + * The test block node. + * + * @var \Drupal\node\NodeInterface + */ + protected $blockNode; + + /** * {@inheritdoc} */ public function setUp() { 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')); @@ -64,6 +74,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. * * @param string $bundle