class RequireOnPublishValidator in Require on Publish 8
Validates the RequireOnPublish constraint.
Hierarchy
- class \Drupal\require_on_publish\Plugin\Validation\Constraint\RequireOnPublishValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface uses StringTranslationTrait
Expanded class hierarchy of RequireOnPublishValidator
File
- src/
Plugin/ Validation/ Constraint/ RequireOnPublishValidator.php, line 18
Namespace
Drupal\require_on_publish\Plugin\Validation\ConstraintView source
class RequireOnPublishValidator extends ConstraintValidator implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a RequireOnPublishValidator object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack object.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(ModuleHandlerInterface $module_handler, RequestStack $request_stack, MessengerInterface $messenger) {
$this->moduleHandler = $module_handler;
$this->messenger = $messenger;
$this->request = $request_stack
->getCurrentRequest();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('request_stack'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint) {
if (!isset($entity)) {
return;
}
$is_published = $entity
->isPublished();
// If the entity is a paragraph, we need to determine the publish status of
// its parent entity.
if ($this->moduleHandler
->moduleExists('paragraphs') && $entity
->getEntityTypeId() == 'paragraph') {
if ($this->request) {
$status = $this->request->request
->get('status', [
'value' => 0,
]);
$is_published = $status['value'];
}
else {
$is_published = $entity
->getParentEntity()
->isPublished();
}
}
/** @var \Drupal\Core\Field\FieldItemListInterface $field */
foreach ($entity
->getFields() as $field) {
/** @var \Drupal\Core\Field\FieldConfigInterface $field_config */
$field_config = $field
->getFieldDefinition();
if (!$field_config instanceof FieldConfigInterface) {
continue;
}
if (!$field
->isEmpty()) {
continue;
}
if (!$field_config
->getThirdPartySetting('require_on_publish', 'require_on_publish', FALSE)) {
continue;
}
if ($is_published) {
$message = $this
->t($constraint->message, [
'%field_label' => $field_config
->getLabel(),
]);
$this->context
->buildViolation($message)
->atPath($field
->getName())
->addViolation();
}
else {
if (!$field_config
->getThirdPartySetting('require_on_publish', 'warn_on_empty', FALSE)) {
continue;
}
$message = $this
->t('%label can not be empty on publication', [
'%label' => $field_config
->getLabel(),
]);
$this->messenger
->addWarning($message);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RequireOnPublishValidator:: |
protected | property | The messenger service. | |
RequireOnPublishValidator:: |
protected | property | The module handler. | |
RequireOnPublishValidator:: |
protected | property | The request object. | |
RequireOnPublishValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
RequireOnPublishValidator:: |
public | function | Checks if the passed value is valid. | |
RequireOnPublishValidator:: |
public | function | Constructs a RequireOnPublishValidator object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |