diff --git a/core/lib/Drupal/Core/Render/Element/Radio.php b/core/lib/Drupal/Core/Render/Element/Radio.php index ae20284..c829723 100644 --- a/core/lib/Drupal/Core/Render/Element/Radio.php +++ b/core/lib/Drupal/Core/Render/Element/Radio.php @@ -54,8 +54,26 @@ public static function preRenderRadio($element) { $element['#attributes']['type'] = 'radio'; Element::setAttributes($element, array('id', 'name', '#return_value' => 'value')); - if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) { - $element['#attributes']['checked'] = 'checked'; + if (isset($element['#return_value'])) { + // To avoid auto-casting during '==' we convert int 0 to '0' for both + // operands. It will prevent wrong true-checking for both cases: 0 == + // 'string' and 'string' == 0. + if ($element['#value'] === 0 || $element['#value'] === FALSE) { + $element['#value'] = '0'; + } + elseif ($element['#value'] === TRUE) { + $element['#value'] = '1'; + } + if ($element['#return_value'] === 0 || $element['#return_value'] === FALSE) { + $element['#return_value'] = '0'; + } + elseif ($element['#return_value'] === TRUE) { + $element['#return_value'] = '1'; + } + + if ($element['#value'] == $element['#return_value']) { + $element['#attributes']['checked'] = 'checked'; + } } static::setAttributes($element, array('form-radio')); diff --git a/core/modules/system/src/Tests/Form/ElementTest.php b/core/modules/system/src/Tests/Form/ElementTest.php index 0ae2edc..0a090aa 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -89,6 +89,26 @@ function testOptions() { } /** + * Tests correct checked attribute for radios element. + */ + function testRadiosChecked() { + // Verify that there is only one radio option checked. + $this->drupalGet('form-test/radios-checked'); + $elements = $this->xpath('//input[@name="radios" and @checked]'); + $this->assertEqual(count($elements), 1); + $this->assertIdentical((string) $elements[0]['value'], '0'); + $elements = $this->xpath('//input[@name="radios-string" and @checked]'); + $this->assertEqual(count($elements), 1); + $this->assertIdentical((string) $elements[0]['value'], 'bar'); + $elements = $this->xpath('//input[@name="radios-boolean-true" and @checked]'); + $this->assertEqual(count($elements), 1); + $this->assertIdentical((string) $elements[0]['value'], '1'); + $elements = $this->xpath('//input[@name="radios-boolean-false" and @checked]'); + $this->assertEqual(count($elements), 1); + $this->assertIdentical((string) $elements[0]['value'], '0'); + } + + /** * Tests wrapper ids for checkboxes and radios. */ function testWrapperIds() { diff --git a/core/modules/system/tests/modules/form_test/form_test.routing.yml b/core/modules/system/tests/modules/form_test/form_test.routing.yml index 790b9ce..5c9f18c 100644 --- a/core/modules/system/tests/modules/form_test/form_test.routing.yml +++ b/core/modules/system/tests/modules/form_test/form_test.routing.yml @@ -294,6 +294,14 @@ form_test.checkboxes_radios: requirements: _access: 'TRUE' +form_test.radios_checked: + path: '/form-test/radios-checked' + defaults: + _form: '\Drupal\form_test\Form\FormTestRadiosCheckedForm' + _title: 'Radios checked defalt value' + requirements: + _access: 'TRUE' + form_test.email: path: '/form-test/email' defaults: diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index 37b6a4c..385689e 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -1702,7 +1702,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { break; case 'link_display': $form['#title'] .= $this->t('Which display to use for path'); - $options = array(FALSE => $this->t('None'), 'custom_url' => $this->t('Custom URL')); + $options = array('' => $this->t('None'), 'custom_url' => $this->t('Custom URL')); foreach ($this->view->storage->get('display') as $display_id => $display) { if ($this->view->displayHandlers->get($display_id)->hasPath()) { diff --git a/core/modules/views_ui/src/Tests/DisplayTest.php b/core/modules/views_ui/src/Tests/DisplayTest.php index 7f5f125..2976120 100644 --- a/core/modules/views_ui/src/Tests/DisplayTest.php +++ b/core/modules/views_ui/src/Tests/DisplayTest.php @@ -163,7 +163,9 @@ public function testLinkDisplay() { $this->assertEqual($result[0], t('None'), 'Make sure that the link option summary shows "None" by default.'); $this->drupalGet($link_display_path); - $this->assertFieldChecked('edit-link-display-0'); + // We are looking for the None option which has a '' key, that's why there + // is no key appended in the ID. + $this->assertFieldChecked('edit-link-display-'); // Test the default radio option on the link display form. $this->drupalPostForm($link_display_path, array('link_display' => 'page_1'), t('Apply')); diff --git a/core/tests/Drupal/Tests/Core/Render/Element/RadioTest.php b/core/tests/Drupal/Tests/Core/Render/Element/RadioTest.php new file mode 100644 index 0000000..7381cfa --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Render/Element/RadioTest.php @@ -0,0 +1,29 @@ + 0, + '#value' => 'All', + '#attributes' => [], + ]; + + $element = Radio::preRenderRadio($element); + + $this->assertArrayNotHasKey('checked', $element['#attributes']); + } + +}