You are here

class RequireOnPublishValidator in Require on Publish 8

Validates the RequireOnPublish constraint.

Hierarchy

Expanded class hierarchy of RequireOnPublishValidator

File

src/Plugin/Validation/Constraint/RequireOnPublishValidator.php, line 18

Namespace

Drupal\require_on_publish\Plugin\Validation\Constraint
View 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

Namesort descending Modifiers Type Description Overrides
RequireOnPublishValidator::$messenger protected property The messenger service.
RequireOnPublishValidator::$moduleHandler protected property The module handler.
RequireOnPublishValidator::$request protected property The request object.
RequireOnPublishValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
RequireOnPublishValidator::validate public function Checks if the passed value is valid.
RequireOnPublishValidator::__construct public function Constructs a RequireOnPublishValidator object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.