diff --git a/core/lib/Drupal/Core/Render/Element/Radio.php b/core/lib/Drupal/Core/Render/Element/Radio.php index 2e8b28c..501da61 100644 --- a/core/lib/Drupal/Core/Render/Element/Radio.php +++ b/core/lib/Drupal/Core/Render/Element/Radio.php @@ -61,8 +61,19 @@ 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']) && $element['#value'] !== FALSE) { + // 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'] = '0'; + } + if ($element['#return_value'] === 0) { + $element['#return_value'] = '0'; + } + 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 1cc6cd8..249ad5e 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -94,6 +94,20 @@ 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'); + } + + /** * 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 d8c5833..9a595d7 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/system/tests/modules/form_test/src/Form/FormTestRadiosCheckedForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestRadiosCheckedForm.php new file mode 100644 index 0000000..e7e594e --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestRadiosCheckedForm.php @@ -0,0 +1,67 @@ + 'radios', + '#title' => 'Radios', + '#options' => array( + 0 => 'Zero', + 'foo' => 'Foo', + 1 => 'One', + 'bar' => 'Bar - radios', + '>' => "Special Char", + ), + '#default_value' => 0, + ); + $form['radios-string'] = array( + '#type' => 'radios', + '#title' => 'Radios', + '#options' => array( + 0 => 'Zero', + 'foo' => 'Foo', + 1 => 'One', + 'bar' => 'Bar - radios', + '>' => "Special Char", + ), + '#default_value' => 'bar', + ); + + $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $form_state->setResponse(new JsonResponse($form_state->getValues())); + } + +} diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index 1d18ee1..cc49773 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -1706,7 +1706,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 4fab7d8..0451045 100644 --- a/core/modules/views_ui/src/Tests/DisplayTest.php +++ b/core/modules/views_ui/src/Tests/DisplayTest.php @@ -168,7 +168,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'));