commit f18e2700d4d41b48a0c935d1c146b7efeda9662a Author: fago Date: Sun Feb 1 15:47:22 2015 +0100 interdiff. diff --git a/core/lib/Drupal/Core/Validation/ConstraintManager.php b/core/lib/Drupal/Core/Validation/ConstraintManager.php index 458c0bc..c760d1e 100644 --- a/core/lib/Drupal/Core/Validation/ConstraintManager.php +++ b/core/lib/Drupal/Core/Validation/ConstraintManager.php @@ -68,7 +68,7 @@ public function create($name, $options) { // Plugins need an array as configuration, so make sure we have one. // The constraint classes support passing the options as part of the // 'value' key also. - $options = array('value' => $options); + $options = isset($options) ? array('value' => $options) : array(); } return $this->createInstance($name, $options); } diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index fc168df..ab32c82 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -88,6 +88,7 @@ public function form(array $form, FormStateInterface $form_state) { // The mail field is NOT required if account originally had no mail set // and the user performing the edit has 'administer users' permission. // This allows users without email address to be edited and deleted. + // Also see \Drupal\user\Plugin\Validation\Constraint\UserMailRequired. $form['account']['mail'] = array( '#type' => 'email', '#title' => $this->t('Email address'), diff --git a/core/modules/user/src/Entity/User.php b/core/modules/user/src/Entity/User.php index df0f353..5c315d9 100644 --- a/core/modules/user/src/Entity/User.php +++ b/core/modules/user/src/Entity/User.php @@ -520,7 +520,8 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setLabel(t('Email')) ->setDescription(t('The email of this user.')) ->setDefaultValue('') - ->setConstraints(array('UserMailUnique' => array())); + ->addConstraint('UserMailUnique') + ->addConstraint('UserMailRequired'); // @todo Convert to a text field in https://drupal.org/node/1548204. $fields['signature'] = BaseFieldDefinition::create('string') diff --git a/core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php b/core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php new file mode 100644 index 0000000..77c08b3 --- /dev/null +++ b/core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php @@ -0,0 +1,77 @@ +context = $context; + } + + /** + * {@inheritdoc} + */ + public function validatedBy() { + return get_class($this); + } + + /** + * {@inheritdoc} + */ + public function validate($items, Constraint $constraint) { + /** @var \Drupal\Core\Field\FieldItemListInterface $items */ + /** @var \Drupal\user\UserInterface $account */ + $account = $this->context->getMetadata()->getTypedData()->getEntity(); + $existing_value = NULL; + if ($account->id()) { + $account_unchanged = \Drupal::entityManager() + ->getStorage('user') + ->loadUnchanged($account->id()); + $existing_value = $account_unchanged->getEmail(); + } + + $required = !(!$existing_value && \Drupal::currentUser()->hasPermission('administer users')); + + if ($required && (!isset($items) || $items->isEmpty())) { + $this->context->addViolation($this->message, array('!name' => String::placeholder($account->getFieldDefinition('mail')->getLabel()))); + } + } + +} diff --git a/core/modules/user/src/Tests/UserValidationTest.php b/core/modules/user/src/Tests/UserValidationTest.php index 25ef677..acc64b7 100644 --- a/core/modules/user/src/Tests/UserValidationTest.php +++ b/core/modules/user/src/Tests/UserValidationTest.php @@ -7,6 +7,7 @@ namespace Drupal\user\Tests; +use Drupal\Component\Utility\String; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem; use Drupal\Core\Language\Language; @@ -75,7 +76,10 @@ function testUsernames() { * Runs entity validation checks. */ function testValidation() { - $user = User::create(array('name' => 'test')); + $user = User::create(array( + 'name' => 'test', + 'mail' => 'test@example.com', + )); $violations = $user->validate(); $this->assertEqual(count($violations), 0, 'No violations when validating a default user.'); @@ -129,6 +133,11 @@ function testValidation() { $this->assertEqual($violations[0]->getPropertyPath(), 'mail'); $this->assertEqual($violations[0]->getMessage(), t('The email address %mail is already taken.', array('%mail' => 'existing@example.com'))); $user->set('mail', NULL); + $violations = $user->validate(); + $this->assertEqual(count($violations), 1, 'E-mail addresses may not be removed'); + $this->assertEqual($violations[0]->getPropertyPath(), 'mail'); + $this->assertEqual($violations[0]->getMessage(), t('!name field is required.', array('!name' => String::placeholder($user->getFieldDefinition('mail')->getLabel())))); + $user->set('mail', 'someone@example.com'); $user->set('signature', $this->randomString(256)); $this->assertLengthViolation($user, 'signature', 255); @@ -168,6 +177,7 @@ function testValidation() { // Test cardinality of user roles. $user = entity_create('user', array( 'name' => 'role_test', + 'mail' => 'test@example.com', 'roles' => array('role1', 'role2'), )); $violations = $user->validate();