You are here

ReadonlymodeConstraintValidator.php in Read only mode 2.0.x

File

src/Plugin/Validation/Constraint/ReadonlymodeConstraintValidator.php
View source
<?php

namespace Drupal\readonlymode\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Validator for the Readonlymode constraint.
 */
class ReadonlymodeConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {

    // Here we could do an early return if the user has the permission to update
    // content anyway. But it will be lost during the deployment so do we want
    // that really?

    /** @var \Drupal\readonlymode\ReadonlymodeManager $manager */
    $manager = \Drupal::service('readonlymode.manager');
    if ($manager
      ->isReadonly() && PHP_SAPI !== 'cli') {

      // Here we could also get the entity from the $items and pass it to the
      // manager to get the error tailored to the entity.
      $this->context
        ->addViolation('@readonly_message', [
        '@readonly_message' => $manager
          ->getErrorMessage(),
      ]);
    }
  }

}

Classes

Namesort descending Description
ReadonlymodeConstraintValidator Validator for the Readonlymode constraint.