class CommentNameConstraintValidator in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php \Drupal\comment\Plugin\Validation\Constraint\CommentNameConstraintValidator
Validates the CommentName constraint.
Hierarchy
- class \Symfony\Component\Validator\ConstraintValidator implements ConstraintValidatorInterface
- class \Drupal\comment\Plugin\Validation\Constraint\CommentNameConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of CommentNameConstraintValidator
File
- core/
modules/ comment/ src/ Plugin/ Validation/ Constraint/ CommentNameConstraintValidator.php, line 20 - Contains \Drupal\comment\Plugin\Validation\Constraint\CommentNameConstraintValidator.
Namespace
Drupal\comment\Plugin\Validation\ConstraintView source
class CommentNameConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* Validator 2.5 and upwards compatible execution context.
*
* @var \Symfony\Component\Validator\Context\ExecutionContextInterface
*/
protected $context;
/**
* User storage handler.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* Constructs a new CommentNameConstraintValidator.
*
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage handler.
*/
public function __construct(UserStorageInterface $user_storage) {
$this->userStorage = $user_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('user'));
}
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint) {
$author_name = $entity->name->value;
$owner_id = (int) $entity->uid->target_id;
// Do not allow unauthenticated comment authors to use a name that is
// taken by a registered user.
if (isset($author_name) && $author_name !== '' && $owner_id === 0) {
$users = $this->userStorage
->loadByProperties(array(
'name' => $author_name,
));
if (!empty($users)) {
$this->context
->buildViolation($constraint->messageNameTaken, array(
'%name' => $author_name,
))
->atPath('name')
->addViolation();
}
}
elseif (isset($author_name) && $author_name !== '' && $owner_id) {
$owner = $this->userStorage
->load($owner_id);
if ($owner
->getUsername() != $author_name) {
$this->context
->buildViolation($constraint->messageMatch)
->atPath('name')
->addViolation();
}
}
// Anonymous account might be required - depending on field settings.
if ($owner_id === 0 && empty($author_name) && $this
->getAnonymousContactDetailsSetting($entity) === COMMENT_ANONYMOUS_MUST_CONTACT) {
$this->context
->buildViolation($constraint->messageRequired)
->atPath('name')
->addViolation();
}
}
/**
* Gets the anonymous contact details setting from the comment.
*
* @param \Drupal\comment\CommentInterface $comment
* The entity.
*
* @return int
* The anonymous contact setting.
*/
protected function getAnonymousContactDetailsSetting(CommentInterface $comment) {
return $comment
->getCommentedEntity()
->get($comment
->getFieldName())
->getFieldDefinition()
->getSetting('anonymous');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommentNameConstraintValidator:: |
protected | property |
Validator 2.5 and upwards compatible execution context. Overrides ConstraintValidator:: |
|
CommentNameConstraintValidator:: |
protected | property | User storage handler. | |
CommentNameConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
CommentNameConstraintValidator:: |
protected | function | Gets the anonymous contact details setting from the comment. | |
CommentNameConstraintValidator:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
CommentNameConstraintValidator:: |
public | function | Constructs a new CommentNameConstraintValidator. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Wrapper for {@link ExecutionContextInterface::buildViolation} that supports the 2.4 context API. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the type of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of the value. | |
ConstraintValidator:: |
protected | function | Returns a string representation of a list of values. | |
ConstraintValidator:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
1 |
ConstraintValidator:: |
constant | Whether to cast objects with a "__toString()" method to strings. | ||
ConstraintValidator:: |
constant | Whether to format {@link \DateTime} objects as RFC-3339 dates ("Y-m-d H:i:s"). |