diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 6976bab..4106381 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -17,7 +17,8 @@ * description = @Translation("An entity field containing a path alias and related data."), * no_ui = TRUE, * default_widget = "path", - * list_class = "\Drupal\path\Plugin\Field\FieldType\PathFieldItemList" + * list_class = "\Drupal\path\Plugin\Field\FieldType\PathFieldItemList", + * constraints = {"PathAlias" = {}}, * ) */ class PathItem extends FieldItemBase { @@ -123,19 +124,13 @@ public function postSave($update) { } } else { - $entity = $this->getEntity(); - // Prevent changes to the URL alias if the entity being saved is not the - // default revision. - if (!$entity->isDefaultRevision() && ($this->pid || $this->alias)) { - drupal_set_message($this->t('You cannot change the URL alias until you publish this @entity_type_label.', ['@entity_type_label' => $entity->getEntityType()->getLabel()]), 'error'); - return; - } // Delete old alias if user erased it. if ($this->pid && !$this->alias) { \Drupal::service('path.alias_storage')->delete(['pid' => $this->pid]); } // Only save a non-empty alias. elseif ($this->alias) { + $entity = $this->getEntity(); \Drupal::service('path.alias_storage')->save('/' . $entity->urlInfo()->getInternalPath(), $this->alias, $this->getLangcode(), $this->pid); } } diff --git a/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraint.php b/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraint.php new file mode 100644 index 0000000..d456cc6 --- /dev/null +++ b/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraint.php @@ -0,0 +1,19 @@ +published version of this content.'; + +} diff --git a/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraintValidator.php b/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraintValidator.php new file mode 100644 index 0000000..986df4b --- /dev/null +++ b/core/modules/path/src/Plugin/Validation/Constraint/PathAliasConstraintValidator.php @@ -0,0 +1,57 @@ +entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) { + $entity = !empty($value->getParent()) ? $value->getEntity() : NULL; + + if ($entity && !$entity->isNew() && !$entity->isDefaultRevision()) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $original */ + $original = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id()); + if ($value->alias != $original->path->alias) { + $this->context->addViolation($constraint->message); + } + } + } + +} diff --git a/core/modules/path/tests/src/Functional/PathContentModerationTest.php b/core/modules/path/tests/src/Functional/PathContentModerationTest.php index 51620b2..9fe5627 100644 --- a/core/modules/path/tests/src/Functional/PathContentModerationTest.php +++ b/core/modules/path/tests/src/Functional/PathContentModerationTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\path\Functional; -use Drupal\Core\Path\AliasStorage; use Drupal\node\Entity\NodeType; use Drupal\Tests\BrowserTestBase; use Drupal\workflows\Entity\Workflow; @@ -10,6 +9,7 @@ /** * Tests path aliases with Content Moderation. * + * @group content_moderation * @group path */ class PathContentModerationTest extends BrowserTestBase { @@ -59,7 +59,7 @@ public function testNodePathAlias() { 'title[0][value]' => 'forward revision', 'path[0][alias]' => '/moderated-content', ], t('Save and Create New Draft')); - $this->assertSession()->pageTextContains('You cannot change the URL alias until you publish this Content.'); + $this->assertSession()->pageTextNotContains('This is not the default revision. You can only change the URL alias for the published version of this content.'); // Create some moderated content with no path alias. $this->drupalGet('node/add/moderated'); @@ -77,7 +77,7 @@ public function testNodePathAlias() { 'title[0][value]' => 'forward revision', 'path[0][alias]' => '/forward-revision', ], t('Save and Create New Draft')); - $this->assertSession()->pageTextContains('You cannot change the URL alias until you publish this Content.'); + $this->assertSession()->pageTextContains('This is not the default revision. You can only change the URL alias for the published version of this content.'); // Create some moderated content with no path alias. $this->drupalGet('node/add/moderated'); @@ -95,7 +95,7 @@ public function testNodePathAlias() { 'title[0][value]' => 'forward revision', 'path[0][alias]' => '', ], t('Save and Create New Draft')); - $this->assertSession()->pageTextNotContains('You cannot change the URL alias until you publish this Content.'); - + $this->assertSession()->pageTextNotContains('This is not the default revision. You can only change the URL alias for the published version of this content.'); } + }