Commit 1b3a1c92 authored by amateescu's avatar amateescu Committed by Berdir
Browse files

Issue #2756703 by MegaChriz, amateescu, jasonawant, esclapes: URL Alias not saving in some cases

parent f4f49ce6
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
namespace Drupal\pathauto; namespace Drupal\pathauto;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
/** /**
* Extends the core path widget. * Extends the core path widget.
...@@ -40,6 +40,11 @@ class PathautoWidget extends PathWidget { ...@@ -40,6 +40,11 @@ class PathautoWidget extends PathWidget {
$pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity); $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
if (empty($pattern)) { if (empty($pattern)) {
// Explicitly turn off pathauto here.
$element['pathauto'] = [
'#type' => 'value',
'#value' => PathautoState::SKIP,
];
return $element; return $element;
} }
......
...@@ -290,4 +290,90 @@ class PathautoNodeWebTest extends BrowserTestBase { ...@@ -290,4 +290,90 @@ class PathautoNodeWebTest extends BrowserTestBase {
$this->assertResponse(200); $this->assertResponse(200);
} }
/**
* Tests that nodes with an automatic alias can get a custom alias.
*/
public function testCustomAliasAfterAutomaticAlias() {
// Create a pattern.
$this->createPattern('node', '/content/[node:title]');
// Create a node with an automatic alias.
$edit = [
'title[0][value]' => 'Sample article',
];
$this->drupalPostForm('node/add/article', $edit, 'Save');
$this->assertSession()->pageTextContains('article Sample article has been created.');
// Ensure that the automatic alias got created.
$this->assertAliasExists(['alias' => '/content/sample-article']);
$this->drupalGet('/content/sample-article');
$this->assertSession()->statusCodeEquals(200);
// Now edit the node, set a custom alias.
$edit = [
'path[0][pathauto]' => 0,
'path[0][alias]' => '/sample-pattern-for-article',
];
$this->drupalPostForm('node/1/edit', $edit, 'Save');
// Assert that the new alias exists and the old one does not.
$this->assertAliasExists(['alias' => '/sample-pattern-for-article']);
$this->assertNoAliasExists(['alias' => '/content/sample-article']);
$this->drupalGet('sample-pattern-for-article');
$this->assertSession()->statusCodeEquals(200);
}
/**
* Tests setting custom alias for nodes after removing pattern.
*
* Tests that nodes that had an automatic alias can get a custom alias after
* the pathauto pattern on which the automatic alias was based, is removed.
*/
public function testCustomAliasAfterRemovingPattern() {
// Create a pattern.
$this->createPattern('node', '/content/[node:title]');
// Create a node with an automatic alias.
$edit = [
'title[0][value]' => 'Sample article',
];
$this->drupalPostForm('node/add/article', $edit, 'Save');
$this->assertSession()->pageTextContains('article Sample article has been created.');
// Ensure that the automatic alias got created.
$this->assertAliasExists(['alias' => '/content/sample-article']);
$this->drupalGet('/content/sample-article');
$this->assertSession()->statusCodeEquals(200);
// Go to the edit the node form and confirm that the pathauto checkbox
// exists.
$this->drupalGet('node/1/edit');
$this->assertSession()->elementExists('css', '#edit-path-0-pathauto');
// Delete all patterns to be sure that there will be no match.
$entity_ids = \Drupal::entityQuery('pathauto_pattern')->execute();
$entities = PathautoPattern::loadMultiple($entity_ids);
foreach ($entities as $entity) {
$entity->delete();
}
// Reload the node edit form and confirm that the pathauto checkbox no
// longer exists.
$this->drupalGet('node/1/edit');
$this->assertSession()->elementNotExists('css', '#edit-path-0-pathauto');
// Set a custom alias. We cannot disable the pathauto checkbox, because
// there is none.
$edit = [
'path[0][alias]' => '/sample-alias-for-article',
];
$this->submitForm($edit, 'Save');
// Check that the new alias exists and the old one does not.
$this->assertAliasExists(['alias' => '/sample-alias-for-article']);
$this->assertNoAliasExists(['alias' => '/content/sample-article']);
$this->drupalGet('sample-alias-for-article');
$this->assertSession()->statusCodeEquals(200);
}
} }
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