Commit cf94492e authored by alexpott's avatar alexpott

Issue #565220 by voleger, Yogesh Pawar, gpk, alexpott: Fix Weight form element behavior

parent 877e4a81
......@@ -59,6 +59,11 @@ public static function processWeight(&$element, FormStateInterface $form_state,
for ($n = (-1 * $element['#delta']); $n <= $element['#delta']; $n++) {
$weights[$n] = $n;
}
$default_value = (int) $element['#default_value'];
if (!isset($weights[$default_value])) {
$weights[$default_value] = $default_value;
ksort($weights);
}
$element['#options'] = $weights;
$element += $element_info_manager->getInfo('select');
}
......
<?php
namespace Drupal\Tests\Core\Render\Element;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\Element\Weight;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\Core\Render\Element\Weight
* @group Render
*/
class WeightTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['system']);
}
/**
* Test existing #default_value value in #options list.
*
* @covers ::processWeight
*/
public function testProcessWeight() {
$element = [];
$form_state = new FormState();
$complete_form = [];
$element_object = new Weight([], 'weight', []);
$info = $element_object->getInfo();
$element += $info;
$element['#default_value'] = $element['#delta'] + 5;
Weight::processWeight($element, $form_state, $complete_form);
$this->assertTrue(
isset($element['#options'][$element['#default_value']]),
'Default value exists in the #options list'
);
}
}
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