You are here

class ReadonlymodeConstraintValidator in Read only mode 2.0.x

Validator for the Readonlymode constraint.

Hierarchy

Expanded class hierarchy of ReadonlymodeConstraintValidator

File

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

Namespace

Drupal\readonlymode\Plugin\Validation\Constraint
View source
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(),
      ]);
    }
  }

}

Members