diff --git a/config/schema/widget.schema.yml b/config/schema/widget.schema.yml new file mode 100644 index 0000000..e410d18 --- /dev/null +++ b/config/schema/widget.schema.yml @@ -0,0 +1,54 @@ +# Block settings schema. +block.settings.widget_block: + type: block_settings + label: 'Widget block' + mapping: + blocks: + type: sequence + label: 'Blocks' + sequence: + - type: widget_block.block + layout: + type: string + label: 'Layout' + classes: + type: sequence + label: 'Classes' + sequence: + - type: string + regions: + type: sequence + label: 'Regions' + sequence: + - type: widget_block.region + +widget_block.block: + type: block.settings.[id] + label: 'Widget block settings' + mapping: + id: + type: string + label: 'Id' + region: + type: string + label: 'Region' + +widget_block.region: + type: mapping + label: 'Widget region settings' + mapping: + id: + type: string + label: 'Id' + region_id: + type: string + label: 'Region id' + label: + type: string + label: 'Label' + weight: + type: integer + label: 'Weight' + uuid: + type: string + label: 'Uuid' diff --git a/src/Plugin/Block/WidgetBlock.php b/src/Plugin/Block/WidgetBlock.php index 96cf1b2..4c8f087 100644 --- a/src/Plugin/Block/WidgetBlock.php +++ b/src/Plugin/Block/WidgetBlock.php @@ -154,10 +154,6 @@ public function build() { */ public function blockForm($form, FormStateInterface $form_state) { // Fish the page object from the form args. - - // Prevent serialization error. - $form['admin_label']['#markup'] = (string) $form['admin_label']['#markup']; - foreach ($form_state->getBuildInfo()['args'] as $arg) { if ($arg instanceof Page) { $this->page = $arg->getExecutable(); @@ -174,7 +170,7 @@ public function blockForm($form, FormStateInterface $form_state) { $widget_blocks = $form_state->hasValue(array('settings', 'blocks')) ? $form_state->getValue(array('settings', 'blocks')) : $this->configuration['blocks']; $layout = $form_state->hasValue(array('settings', 'layout')) ? $form_state->getValue(array('settings', 'layout')) : $this->configuration['layout']; - $classes = $form_state->hasValue(array('settings', 'classes')) ? $form_state->getValue(array('settings', 'classes')) : $this->configuration['classes']; + $classes = $form_state->hasValue(array('settings', 'classes')) && !empty($form_state->getValue(array('settings', 'classes'))) ? $form_state->getValue(array('settings', 'classes')) : $this->configuration['classes']; $ajax_properties = array( '#ajax' => array( @@ -210,7 +206,8 @@ public function blockForm($form, FormStateInterface $form_state) { $form['classes'] = array( '#type' => 'textfield', '#title' => t('CSS Classes'), - '#default_value' => $classes, + '#default_value' => implode(", ", $this->configuration['classes']), + '#description' => t("A list of CSS Classes that will be applied in the widget. Put each on comma separated."), ); if (!$layout) { @@ -269,7 +266,7 @@ public function blockForm($form, FormStateInterface $form_state) { public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['blocks'] = array(); $this->configuration['layout'] = $form_state->getValue('layout'); - $this->configuration['classes'] = $form_state->getValue('classes'); + $this->configuration['classes'] = array_filter(explode(",", $form_state->getValue('classes')), 'trim'); // Set empty block ID's to NULL. foreach ($form_state->getValue('blocks') as $region_id => $block) { if (!empty($block['id'])) { diff --git a/src/Tests/WidgetBlockTest.php b/src/Tests/WidgetBlockTest.php new file mode 100644 index 0000000..fe137ee --- /dev/null +++ b/src/Tests/WidgetBlockTest.php @@ -0,0 +1,59 @@ +drupalPlaceBlock('widget_block'); + $admin = $this->drupalCreateUser([ + 'administer blocks', + 'administer pages', + ], $this->randomMachineName(), TRUE); + $this->drupalLogin($admin); + } + + /** + * Tests a form without submit handler. + */ + public function testFormNoSubmitHandler() { + $edit = [ + 'settings[layout]' => 'widget_1col', + ]; + $this->drupalPostAjaxForm('admin/structure/block/add/widget_block/classy?region=content', $edit, 'settings[layout]'); + $edit = [ + 'settings[layout]' => 'widget_1col', + 'settings[blocks][links][id]' => 'system_menu_block:main', + 'settings[blocks][main][id]' => 'system_main_block', + ]; + $this->drupalPostForm(NULL, $edit, 'Save block'); + $this->assertText(t('The block configuration has been saved.')); + $this->drupalPlaceBlock('widget_block'); + $this->drupalGet(''); + } + +}