Commit 0b563976 authored by webchick's avatar webchick

Issue #2082509 by Gábor Hojtsy, twistor: Generalize node changed constraint to...

Issue #2082509 by Gábor Hojtsy, twistor: Generalize node changed constraint to entity changed constraint.
parent d72692c6
......@@ -2,24 +2,25 @@
/**
* @file
* Contains \Drupal\node\Plugin\Validation\Constraint\NodeChangedConstraint.
* Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\EntityChangedConstraint.
*/
namespace Drupal\node\Plugin\Validation\Constraint;
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Symfony\Component\Validator\Constraint;
/**
* Validation constraint for the node changed timestamp.
* Validation constraint for the entity changed timestamp.
*
* @Plugin(
* id = "NodeChanged",
* label = @Translation("Node changed", context = "Validation")
* id = "EntityChanged",
* label = @Translation("Entity changed", context = "Validation"),
* type = {"entity"}
* )
*/
class NodeChangedConstraint extends Constraint {
class EntityChangedConstraint extends Constraint {
public $message = 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.';
}
......@@ -2,18 +2,19 @@
/**
* @file
* Contains \Drupal\node\Plugin\Validation\Constraint\NodeChangedConstraintValidator.
* Contains \Drupal\Core\Validation\Plugin\Validation\Constraint\EntityChangedConstraintValidator.
*/
namespace Drupal\node\Plugin\Validation\Constraint;
namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
use Drupal\Core\Entity\EntityChangedInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the NodeChanged constraint.
* Validates the EntityChanged constraint.
*/
class NodeChangedConstraintValidator extends ConstraintValidator {
class EntityChangedConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
......@@ -21,13 +22,16 @@ class NodeChangedConstraintValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (isset($value)) {
// We are on the field item level, so we need to go two levels up for the
// node object.
$node = $this->context->getMetadata()->getTypedData()->getParent()->getParent();
$id = $node->id();
$language = $node->language();
if ($id && (node_last_changed($id, $language->id) > $value)) {
$this->context->addViolation($constraint->message);
// entity object.
$entity = $this->context->getMetadata()->getTypedData()->getParent()->getParent();
if (!$entity->isNew()) {
$saved_entity = \Drupal::entityManager()->getStorageController($entity->entityType())->loadUnchanged($entity->id());
if ($saved_entity && ($saved_entity instanceof EntityChangedInterface) && ($saved_entity->getChangedTime() > $value)) {
$this->context->addViolation($constraint->message);
}
}
}
}
}
......@@ -376,7 +376,7 @@ public static function baseFieldDefinitions($entity_type) {
'description' => t('The time that the node was last edited.'),
'type' => 'integer_field',
'property_constraints' => array(
'value' => array('NodeChanged' => array()),
'value' => array('EntityChanged' => array()),
),
);
$properties['comment'] = array(
......
......@@ -253,6 +253,9 @@ public static function baseFieldDefinitions($entity_type) {
'label' => t('Changed'),
'description' => t('The time that the term was last edited.'),
'type' => 'integer_field',
'property_constraints' => array(
'value' => array('EntityChanged' => array()),
),
);
return $properties;
}
......
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