Skip to content
Snippets Groups Projects

Resolve #3500169 "Skip validations"

2 files
+ 29
4
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -3,7 +3,9 @@
namespace Drupal\fapi_validation;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\fapi_validation\Exception\ValidatorException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
@@ -26,8 +28,18 @@ class FapiValidationValidatorsManager extends DefaultPluginManager {
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
public function __construct(
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
protected readonly ConfigFactoryInterface $configFactory,
protected readonly AccountInterface $currentUser
) {
$this->alterInfo('fapi_validation_validators_info');
$this->setCacheBackend($cache_backend, 'fapi_validation_validators');
@@ -65,7 +77,15 @@ class FapiValidationValidatorsManager extends DefaultPluginManager {
* @throws \Drupal\fapi_validation\Exception\ValidatorException
*/
public function validate(array &$element, FormStateInterface $form_state) {
// If element is empty and not required, by pass rule validation.
// If the bypass config is true and the user has appropriate permissions, bypass validation.
$config = $this->configFactory->getEditable('fapi_validation.settings');
$bypass_config = $config->get('bypass');
$current_user = $this->currentUser;
if ($bypass_config && $current_user->hasPermission('bypass fapi validations')) {
return;
}
// If element is empty and not required, bypass rule validation.
$value = !isset($element['#value']) || trim($element['#value']) === '';
if (!$element['#required'] && $value) {
return;
@@ -81,6 +101,10 @@ class FapiValidationValidatorsManager extends DefaultPluginManager {
throw new ValidatorException("Invalid validator name '{$validator->getName()}'.");
}
if ($bypass_config && $current_user->hasPermission("bypass {$validator->getName()} fapi validation")) {
return;
}
$plugin = $this->getDefinition($validator->getName());
$instance = $this->createInstance($plugin['id']);
Loading