diff --git a/core/modules/path/src/Form/PathFormBase.php b/core/modules/path/src/Form/PathFormBase.php index bee68702f45c147b655acb2e2a8588cc04ec6a2f..e3e4c93983c5e92ba3d52ffb3718b620e5d99b64 100644 --- a/core/modules/path/src/Form/PathFormBase.php +++ b/core/modules/path/src/Form/PathFormBase.php @@ -104,7 +104,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $pid = NU '#default_value' => $this->path['alias'], '#maxlength' => 255, '#size' => 45, - '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'), + '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path.'), '#field_prefix' => $this->url('<none>', [], ['absolute' => TRUE]), '#required' => TRUE, ); @@ -151,6 +151,8 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $source = &$form_state->getValue('source'); $source = $this->aliasManager->getPathByAlias($source); $alias = $form_state->getValue('alias'); + // Trim the submitted value of whitespace and slashes. + $alias = trim(trim($alias), " \\/"); // Language is only set if language.module is enabled, otherwise save for all // languages. $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED); @@ -174,6 +176,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $source = &$form_state->getValue('source'); $source = $this->aliasManager->getPathByAlias($source); $alias = $form_state->getValue('alias'); + // Trim the submitted value of whitespace and slashes. + $alias = trim(trim($alias), " \\/"); // Language is only set if language.module is enabled, otherwise save for all // languages. $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED); diff --git a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php index 513aee4037f727b64e1b18e74793fb5c318e9136..6107b182f87f3ba4f575a404d9d50d4d3426c048 100644 --- a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php +++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php @@ -58,7 +58,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#default_value' => $path['alias'], '#required' => $element['#required'], '#maxlength' => 255, - '#description' => $this->t('The alternative URL for this content. Use a relative path without a trailing slash. For example, enter "about" for the about page.'), + '#description' => $this->t('The alternative URL for this content. Use a relative path. For example, enter "about" for the about page.'), ); $element['pid'] = array( '#type' => 'value', @@ -84,8 +84,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen * The form state. */ public static function validateFormElement(array &$element, FormStateInterface $form_state) { - // Trim the submitted value. - $alias = trim($element['alias']['#value']); + // Trim the submitted value of whitespace and slashes. + $alias = trim(trim($element['alias']['#value']), " \\/"); if (!empty($alias)) { $form_state->setValueForElement($element['alias'], $alias); diff --git a/core/modules/path/src/Tests/PathAliasTest.php b/core/modules/path/src/Tests/PathAliasTest.php index 134f9d7762f7e606d8ad94f63de9698ff4170405..f1dc2a589bf0ebe9a90733330896ac41b62f0385 100644 --- a/core/modules/path/src/Tests/PathAliasTest.php +++ b/core/modules/path/src/Tests/PathAliasTest.php @@ -130,6 +130,34 @@ function testAdminAlias() { $this->assertNoText($alias, 'The untruncated alias was not found.'); // The 'truncated' alias will always be found. $this->assertText($truncated_alias, 'The truncated alias was found.'); + + // Create third test node. + $node3 = $this->drupalCreateNode(); + + // Create absolute path alias. + $edit = array(); + $edit['source'] = 'node/' . $node3->id(); + $edit['alias'] = '/' . $this->randomMachineName(8); + $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save')); + + // Confirm that the alias was converted to a relative path. + $this->assertNoText($edit['alias'], 'The absolute alias was not found.'); + // The 'relative' alias will always be found. + $this->assertText(trim($edit['alias'], '/'), 'The relative alias was found.'); + + // Create fourth test node. + $node4 = $this->drupalCreateNode(); + + // Create alias with trailing slash. + $edit = array(); + $edit['source'] = 'node/' . $node4->id(); + $edit['alias'] = $this->randomMachineName(8) . '/'; + $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save')); + + // Confirm that the alias with trailing slash is not found. + $this->assertNoText($edit['alias'], 'The absolute alias was not found.'); + // The alias without trailing flash is found. + $this->assertText(trim($edit['alias'], '/'), 'The alias without trailing slash was found.'); } /** @@ -189,6 +217,30 @@ function testNodeAlias() { $this->drupalGet($edit['path[0][alias]']); $this->assertNoText($node1->label(), 'Alias was successfully deleted.'); $this->assertResponse(404); + + // Create third test node. + $node3 = $this->drupalCreateNode(); + + // Set its path alias to an absolute path. + $edit = array('path[0][alias]' => '/' . $this->randomMachineName(8)); + $this->drupalPostForm('node/' . $node3->id() . '/edit', $edit, t('Save')); + + // Confirm that the alias was converted to a relative path. + $this->drupalGet(trim($edit['path[0][alias]'], '/')); + $this->assertText($node3->label(), 'Alias became relative.'); + $this->assertResponse(200); + + // Create fourth test node. + $node4 = $this->drupalCreateNode(); + + // Set its path alias to have a trailing slash. + $edit = array('path[0][alias]' => $this->randomMachineName(8) . '/'); + $this->drupalPostForm('node/' . $node4->id() . '/edit', $edit, t('Save')); + + // Confirm that the alias was converted to a relative path. + $this->drupalGet(trim($edit['path[0][alias]'], '/')); + $this->assertText($node4->label(), 'Alias trimmed trailing slash.'); + $this->assertResponse(200); } /**