You are here

public function ReadonlymodeConstraintValidator::validate in Read only mode 2.0.x

File

src/Plugin/Validation/Constraint/ReadonlymodeConstraintValidator.php, line 16

Class

ReadonlymodeConstraintValidator
Validator for the Readonlymode constraint.

Namespace

Drupal\readonlymode\Plugin\Validation\Constraint

Code

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(),
    ]);
  }
}