Skip to content
Snippets Groups Projects
Commit bdc44d7b authored by howard ge's avatar howard ge
Browse files

Coding standard for several files.

parent 415ef080
No related branches found
No related tags found
No related merge requests found
...@@ -47,7 +47,7 @@ class FieldValidationRule extends Plugin { ...@@ -47,7 +47,7 @@ class FieldValidationRule extends Plugin {
* *
* @ingroup plugin_translatable * @ingroup plugin_translatable
* *
* @var \Drupal\Core\Annotation\Translation (optional) * @var \Drupal\Core\Annotation\Translation
*/ */
public $description = ''; public $description = '';
......
...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase; ...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface; use Drupal\field_validation\FieldValidationRuleSetInterface;
/** /**
* PhoneFieldValidationRule. * Phone Field Validation Rule.
* *
* @FieldValidationRule( * @FieldValidationRule(
* id = "phone_field_validation_rule", * id = "phone_field_validation_rule",
...@@ -48,7 +48,7 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -48,7 +48,7 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase {
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$countries = $this->phone_countries(); $countries = $this->phone_countries();
$country_options = array(); $country_options = [];
foreach ($countries as $country_code => $country) { foreach ($countries as $country_code => $country) {
$country_options[$country_code] = $country['name'] ?? ''; $country_options[$country_code] = $country['name'] ?? '';
} }
...@@ -70,27 +70,33 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -70,27 +70,33 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$this->configuration['country'] = $form_state->getValue('country'); $this->configuration['country'] = $form_state->getValue('country');
} }
/**
* {@inheritdoc}
*/
public function validate($params) { public function validate($params) {
$value = $params['value'] ?? ''; $value = $params['value'] ?? '';
$rule = $params['rule'] ?? null; $rule = $params['rule'] ?? NULL;
$context = $params['context'] ?? null; $context = $params['context'] ?? NULL;
$settings = []; $settings = [];
if(!empty($rule) && !empty($rule->configuration)){ if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration; $settings = $rule->configuration;
} }
if ($value !== '' && !is_null($value)) { if ($value !== '' && !is_null($value)) {
$country_code = isset($settings['country']) ? $settings['country'] : ''; $country_code = $settings['country'] ?? '';
$country_regex = ''; $country_regex = '';
$countries = $this->phone_countries(); $countries = $this->phoneCountries();
$country_regex = isset($countries[$country_code]['regex']) ? $countries[$country_code]['regex'] : ''; $country_regex = $countries[$country_code]['regex'] ?? '';
if (!preg_match($country_regex, $value)) { if (!preg_match($country_regex, $value)) {
$context->addViolation($rule->getErrorMessage()); $context->addViolation($rule->getErrorMessage());
} }
} }
} }
public function phone_countries() { /**
* Phone regex of countries.
*/
public function phoneCountries() {
$countries = [ $countries = [
'fr' => [ 'fr' => [
'name' => $this->t('France'), 'name' => $this->t('France'),
...@@ -132,7 +138,7 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -132,7 +138,7 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase {
'name' => $this->t('Russia'), 'name' => $this->t('Russia'),
'regex' => "/^\D*[78]?\D*\d{3,5}\D*\d{1,3}\D*\d{2}\D*\d{2}\D*/x", 'regex' => "/^\D*[78]?\D*\d{3,5}\D*\d{1,3}\D*\d{2}\D*\d{2}\D*/x",
], ],
'es' =>[ 'es' => [
'name' => $this->t('Spain'), 'name' => $this->t('Spain'),
'regex' => '/^[0-9]{2,3}-? ?[0-9]{6,7}$/', 'regex' => '/^[0-9]{2,3}-? ?[0-9]{6,7}$/',
], ],
...@@ -200,4 +206,5 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -200,4 +206,5 @@ class PhoneFieldValidationRule extends ConfigurableFieldValidationRuleBase {
return $countries; return $countries;
} }
} }
...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase; ...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface; use Drupal\field_validation\FieldValidationRuleSetInterface;
/** /**
* PlainTextFieldValidationRule. * Plain Text Field Validation Rule.
* *
* @FieldValidationRule( * @FieldValidationRule(
* id = "plain_text_field_validation_rule", * id = "plain_text_field_validation_rule",
...@@ -45,7 +45,6 @@ class PlainTextFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -45,7 +45,6 @@ class PlainTextFieldValidationRule extends ConfigurableFieldValidationRuleBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form; return $form;
} }
...@@ -57,13 +56,17 @@ class PlainTextFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -57,13 +56,17 @@ class PlainTextFieldValidationRule extends ConfigurableFieldValidationRuleBase {
} }
/**
* {@inheritdoc}
*/
public function validate($params) { public function validate($params) {
$value = $params['value'] ?? ''; $value = $params['value'] ?? '';
$rule = $params['rule'] ?? null; $rule = $params['rule'] ?? NULL;
$context = $params['context'] ?? null; $context = $params['context'] ?? NULL;
if ($value != '' && (strcmp($value, strip_tags($value)))) { if ($value != '' && (strcmp($value, strip_tags($value)))) {
$context->addViolation($rule->getErrorMessage()); $context->addViolation($rule->getErrorMessage());
} }
} }
} }
...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase; ...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface; use Drupal\field_validation\FieldValidationRuleSetInterface;
/** /**
* RegexFieldValidationRule. * Regex Field Validation Rule.
* *
* @FieldValidationRule( * @FieldValidationRule(
* id = "regex_field_validation_rule", * id = "regex_field_validation_rule",
...@@ -47,13 +47,13 @@ class RegexFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -47,13 +47,13 @@ class RegexFieldValidationRule extends ConfigurableFieldValidationRuleBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['setting'] = array( $form['setting'] = [
'#type' => 'textarea', '#type' => 'textarea',
'#title' => $this->t('Pattern'), '#title' => $this->t('Pattern'),
'#description' => $this->t('Specify the Perl-compatible regular expression pattern to validate the user input against.'), '#description' => $this->t('Specify the Perl-compatible regular expression pattern to validate the user input against.'),
'#default_value' => $this->configuration['setting'], '#default_value' => $this->configuration['setting'],
'#required' => TRUE, '#required' => TRUE,
); ];
return $form; return $form;
} }
...@@ -67,17 +67,21 @@ class RegexFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -67,17 +67,21 @@ class RegexFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$this->configuration['setting'] = $form_state->getValue('setting'); $this->configuration['setting'] = $form_state->getValue('setting');
} }
/**
* {@inheritdoc}
*/
public function validate($params) { public function validate($params) {
$value = $params['value'] ?? ''; $value = $params['value'] ?? '';
$rule = $params['rule'] ?? null; $rule = $params['rule'] ?? NULL;
$context = $params['context'] ?? null; $context = $params['context'] ?? NULL;
$settings = []; $settings = [];
if(!empty($rule) && !empty($rule->configuration)){ if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration; $settings = $rule->configuration;
} }
$pattern = isset($settings['setting']) ? $settings['setting'] : ''; $pattern = $settings['setting'] ?? '';
if ($value != '' && (!preg_match($pattern, $value))) { if ($value != '' && (!preg_match($pattern, $value))) {
$context->addViolation($rule->getErrorMessage()); $context->addViolation($rule->getErrorMessage());
} }
} }
} }
...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase; ...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface; use Drupal\field_validation\FieldValidationRuleSetInterface;
/** /**
* UniqueFieldValidationRule. * Unique Field Validation Rule.
* *
* @FieldValidationRule( * @FieldValidationRule(
* id = "unique_field_validation_rule", * id = "unique_field_validation_rule",
...@@ -62,7 +62,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -62,7 +62,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$rule_set = $form_state->getBuildInfo()['args'][0]; $rule_set = $form_state->getBuildInfo()['args'][0];
$entity_type_id = $rule_set->getAttachedEntityType(); $entity_type_id = $rule_set->getAttachedEntityType();
$entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id, false); $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id, FALSE);
if ($entity_type->getKey('published')) { if ($entity_type->getKey('published')) {
$form['published'] = [ $form['published'] = [
...@@ -90,26 +90,28 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -90,26 +90,28 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$this->configuration['scope'] = $form_state->getValue('scope'); $this->configuration['scope'] = $form_state->getValue('scope');
$this->configuration['published'] = $form_state->getValue('published') ?: FALSE; $this->configuration['published'] = $form_state->getValue('published') ?: FALSE;
$this->configuration['per_user'] = $form_state->getValue('per_user') ?: FALSE; $this->configuration['per_user'] = $form_state->getValue('per_user') ?: FALSE;
} }
/**
* {@inheritdoc}
*/
public function validate($params) { public function validate($params) {
$value = $params['value'] ?? ''; $value = $params['value'] ?? '';
$rule = $params['rule'] ?? null; $rule = $params['rule'] ?? NULL;
$context = $params['context'] ?? null; $context = $params['context'] ?? NULL;
$items = $params['items'] ?? []; $items = $params['items'] ?? [];
$delta = $params['delta'] ?? ''; $delta = $params['delta'] ?? '';
$column = $rule->getColumn(); $column = $rule->getColumn();
$settings = []; $settings = [];
if(!empty($rule) && !empty($rule->configuration)){ if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration; $settings = $rule->configuration;
} }
$flag = TRUE; $flag = TRUE;
$scope = isset($settings['scope']) ? $settings['scope'] : ''; $scope = $settings['scope'] ?? '';
$published = $settings['published'] ?? FALSE; $published = $settings['published'] ?? FALSE;
$per_user = $settings['per_user'] ?? FALSE; $per_user = $settings['per_user'] ?? FALSE;
$count = 0; $count = 0;
foreach ($items as $delta1 => $item1) { foreach ($items as $delta1 => $item1) {
if ($delta != $delta1) { if ($delta != $delta1) {
...@@ -128,23 +130,23 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -128,23 +130,23 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$query->accessCheck(FALSE); $query->accessCheck(FALSE);
if ($scope == 'bundle') { if ($scope == 'bundle') {
$bundle = $entity->bundle(); $bundle = $entity->bundle();
$bundle_key = $entity->getEntityType()->getKey('bundle'); $bundle_key = $entity->getEntityType()->getKey('bundle');
if(!empty($bundle_key)){ if (!empty($bundle_key)) {
$query->condition($bundle_key, $bundle); $query->condition($bundle_key, $bundle);
} }
} }
if ($published) { if ($published) {
$published_key = $entity->getEntityType()->getKey('published'); $published_key = $entity->getEntityType()->getKey('published');
if(!empty($published_key)){ if (!empty($published_key)) {
$query->condition($published_key, 1); $query->condition($published_key, 1);
} }
} }
if ($per_user) { if ($per_user) {
$owner_key = $entity->getEntityType()->getKey('owner'); $owner_key = $entity->getEntityType()->getKey('owner');
if(!empty($owner_key)){ if (!empty($owner_key)) {
$query->condition($owner_key, \Drupal::currentUser()->id()); $query->condition($owner_key, \Drupal::currentUser()->id());
} }
} }
...@@ -154,7 +156,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -154,7 +156,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase {
$field_name = $items->getFieldDefinition()->getName(); $field_name = $items->getFieldDefinition()->getName();
if(!empty($column)){ if (!empty($column)) {
$field_name = $field_name . '.' . $column; $field_name = $field_name . '.' . $column;
} }
$query->condition($field_name, $value); $query->condition($field_name, $value);
...@@ -171,7 +173,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -171,7 +173,7 @@ class UniqueFieldValidationRule extends ConfigurableFieldValidationRuleBase {
if (!$flag) { if (!$flag) {
$context->addViolation($rule->getErrorMessage()); $context->addViolation($rule->getErrorMessage());
} }
} }
} }
...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase; ...@@ -7,7 +7,7 @@ use Drupal\field_validation\ConfigurableFieldValidationRuleBase;
use Drupal\field_validation\FieldValidationRuleSetInterface; use Drupal\field_validation\FieldValidationRuleSetInterface;
/** /**
* WordsFieldValidationRule. * Words Field Validation Rule.
* *
* @FieldValidationRule( * @FieldValidationRule(
* id = "words_field_validation_rule", * id = "words_field_validation_rule",
...@@ -79,15 +79,15 @@ class WordsFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -79,15 +79,15 @@ class WordsFieldValidationRule extends ConfigurableFieldValidationRuleBase {
public function validate($params) { public function validate($params) {
$value = $params['value'] ?? ''; $value = $params['value'] ?? '';
$rule = $params['rule'] ?? null; $rule = $params['rule'] ?? NULL;
$context = $params['context'] ?? null; $context = $params['context'] ?? NULL;
$settings = []; $settings = [];
if(!empty($rule) && !empty($rule->configuration)){ if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration; $settings = $rule->configuration;
} }
if ($value != '') { if ($value != '') {
$flag = TRUE; $flag = TRUE;
$length =count(explode(' ', trim(preg_replace('/\s+/', ' ', str_replace('&nbsp;', ' ', (strip_tags(str_replace('<', ' <', $value)))))))); $length = count(explode(' ', trim(preg_replace('/\s+/', ' ', str_replace('&nbsp;', ' ', (strip_tags(str_replace('<', ' <', $value))))))));
if (isset($settings['min']) && $settings['min'] != '') { if (isset($settings['min']) && $settings['min'] != '') {
$min = $settings['min']; $min = $settings['min'];
if ($length < $min) { if ($length < $min) {
...@@ -99,11 +99,12 @@ class WordsFieldValidationRule extends ConfigurableFieldValidationRuleBase { ...@@ -99,11 +99,12 @@ class WordsFieldValidationRule extends ConfigurableFieldValidationRuleBase {
if ($length > $max) { if ($length > $max) {
$flag = FALSE; $flag = FALSE;
} }
} }
if (!$flag) { if (!$flag) {
$context->addViolation($rule->getErrorMessage()); $context->addViolation($rule->getErrorMessage());
} }
} }
} }
} }
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace Drupal\field_validation\Plugin\Validation\Constraint; namespace Drupal\field_validation\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\MissingOptionsException;
/** /**
* Checks that the node. * Checks that the node.
...@@ -14,27 +13,28 @@ use Symfony\Component\Validator\Exception\MissingOptionsException; ...@@ -14,27 +13,28 @@ use Symfony\Component\Validator\Exception\MissingOptionsException;
* ) * )
*/ */
class FieldValidationConstraint extends Constraint { class FieldValidationConstraint extends Constraint {
public $ruleset_name; public $ruleset_name;
public $rule_uuid; public $rule_uuid;
public function __construct($options = null){ public function __construct($options = null){
if(is_array($options)){ if (is_array($options)) {
if(key_exists('ruleset_name',$options)){ if (key_exists('ruleset_name',$options)) {
$this->ruleset_name = $options['ruleset_name']; $this->ruleset_name = $options['ruleset_name'];
}
if(key_exists('rule_uuid',$options)) {
$this->rule_uuid = $options['rule_uuid'];
}
} }
if (null !== $options && !is_array($options)) { if (key_exists('rule_uuid',$options)) {
$options = [ $this->rule_uuid = $options['rule_uuid'];
'ruleset_name' => $options, }
'rule_uuid' => $options, }
];
}
parent::__construct($options); if (null !== $options && !is_array($options)) {
$options = [
'ruleset_name' => $options,
'rule_uuid' => $options,
];
} }
parent::__construct($options);
}
} }
...@@ -18,10 +18,11 @@ class FieldValidationConstraintValidator extends ConstraintValidator { ...@@ -18,10 +18,11 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
public function validate($items, Constraint $constraint) { public function validate($items, Constraint $constraint) {
$ruleset_name = $constraint->ruleset_name; $ruleset_name = $constraint->ruleset_name;
$ruleset = \Drupal::entityTypeManager()->getStorage('field_validation_rule_set')->load($ruleset_name); $ruleset = \Drupal::entityTypeManager()->getStorage('field_validation_rule_set')->load($ruleset_name);
if(empty($ruleset)){ if (empty($ruleset)) {
return; return;
} }
//for base field validation, we limit it to attached bundle.
//For base field validation, we limit it to attached bundle.
$entity = $items->getEntity(); $entity = $items->getEntity();
$bundle = $entity->bundle(); $bundle = $entity->bundle();
...@@ -39,12 +40,12 @@ class FieldValidationConstraintValidator extends ConstraintValidator { ...@@ -39,12 +40,12 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
$rules_available = []; $rules_available = [];
$field_name = $items->getFieldDefinition()->getName(); $field_name = $items->getFieldDefinition()->getName();
foreach($rules as $rule){ foreach ($rules as $rule) {
if($rule->getFieldName() == $field_name){ if ($rule->getFieldName() == $field_name) {
$rules_available[] = $rule; $rules_available[] = $rule;
} }
} }
if(empty($rules_available)){ if (empty($rules_available)) {
return; return;
} }
...@@ -52,10 +53,10 @@ class FieldValidationConstraintValidator extends ConstraintValidator { ...@@ -52,10 +53,10 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
$params['items'] = $items; $params['items'] = $items;
$params['context'] = $this->context; $params['context'] = $this->context;
if ($items->count() !== 0) { if ($items->count() !== 0) {
foreach($items as $delta => $item){ foreach ($items as $delta => $item) {
$validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule'); $validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule');
// You can hard code configuration or you load from settings. // You can hard code configuration or you load from settings.
foreach($rules_available as $rule) { foreach ($rules_available as $rule) {
$column = $rule->getColumn(); $column = $rule->getColumn();
$value = $item->{$column}; $value = $item->{$column};
$params['value'] = $value; $params['value'] = $value;
...@@ -67,6 +68,7 @@ class FieldValidationConstraintValidator extends ConstraintValidator { ...@@ -67,6 +68,7 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
$plugin_validator->validate($params); $plugin_validator->validate($params);
} }
} }
}else { }else {
$validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule'); $validator_manager = \Drupal::service('plugin.manager.field_validation.field_validation_rule');
// You can hard code configuration or you load from settings. // You can hard code configuration or you load from settings.
...@@ -81,4 +83,5 @@ class FieldValidationConstraintValidator extends ConstraintValidator { ...@@ -81,4 +83,5 @@ class FieldValidationConstraintValidator extends ConstraintValidator {
} }
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment