Commit ea28d669 authored by xjm's avatar xjm

Issue #2787641 by pwolanin, YesCT: Add non-UI mechanism for setting block...

Issue #2787641 by pwolanin, YesCT: Add non-UI mechanism for setting block weight through block forms
parent bde19066
......@@ -180,6 +180,13 @@ public function form(array $form, FormStateInterface $form_state) {
);
}
// Hidden weight setting.
$weight = $entity->isNew() ? $this->getRequest()->query->get('weight', 0) : $entity->getWeight();
$form['weight'] = array(
'#type' => 'hidden',
'#default_value' => $weight,
);
// Region settings.
$entity_region = $entity->getRegion();
$region = $entity->isNew() ? $this->getRequest()->query->get('region', $entity_region) : $entity_region;
......@@ -294,6 +301,7 @@ protected function actions(array $form, FormStateInterface $form_state) {
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$form_state->setValue('weight', (int) $form_state->getValue('weight'));
// The Block Entity form puts all block plugin form elements in the
// settings form element, so just pass that to the block for validation.
$this->getPluginForm($this->entity->getPlugin())->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
......
......@@ -119,6 +119,7 @@ public function listBlocks(Request $request, $theme) {
$definitions = $this->blockManager->getSortedDefinitions($definitions);
$region = $request->query->get('region');
$weight = $request->query->get('weight');
$rows = [];
foreach ($definitions as $plugin_id => $plugin_definition) {
$row = [];
......@@ -144,6 +145,9 @@ public function listBlocks(Request $request, $theme) {
if ($region) {
$links['add']['query']['region'] = $region;
}
if (isset($weight)) {
$links['add']['query']['weight'] = $weight;
}
$destination = $this->redirectDestination->get();
if ($destination) {
$links['add']['query']['destination'] = $destination;
......
......@@ -171,6 +171,51 @@ public function testAddBlockFromLibrary() {
$this->assertEqual($title, $block->label(), 'Found the block with expected title.');
}
/**
* Tests adding a block from the library page with a weight query string.
*/
public function testAddBlockFromLibraryWithWeight() {
$default_theme = $this->config('system.theme')->get('default');
// Test one positive, zero, and one negative weight.
foreach (['7', '0', '-9'] as $weight) {
$options = [
'query' => [
'region' => 'sidebar_first',
'weight' => $weight,
],
];
$this->drupalGet(Url::fromRoute('block.admin_library', ['theme' => $default_theme], $options));
$block_name = 'system_powered_by_block';
$add_url = Url::fromRoute('block.admin_add', [
'plugin_id' => $block_name,
'theme' => $default_theme
]);
$links = $this->xpath('//a[contains(@href, :href)]', [':href' => $add_url->toString()]);
$this->assertEqual(1, count($links), 'Found one matching link.');
$this->assertEqual(t('Place block'), (string) $links[0], 'Found the expected link text.');
list($path, $query_string) = explode('?', $links[0]['href'], 2);
parse_str($query_string, $query_parts);
$this->assertEqual($weight, $query_parts['weight'], 'Found the expected weight query string.');
// Create a random title for the block.
$title = $this->randomMachineName(8);
$block_id = strtolower($this->randomMachineName(8));
$edit = [
'id' => $block_id,
'settings[label]' => $title,
];
// Create the block using the link parsed from the library page.
$this->drupalPostForm($this->getAbsoluteUrl($links[0]['href']), $edit, t('Save block'));
// Ensure that the block was created with the expected weight.
/** @var \Drupal\block\BlockInterface $block */
$block = Block::load($block_id);
$this->assertEqual($weight, $block->getWeight(), 'Found the block with expected weight.');
}
}
/**
* Test configuring and moving a module-define block to specific regions.
*/
......
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