field_validation.module in Field Validation 8
Same filename and directory in other branches
field validation.
File
field_validation.moduleView source
<?php
use Drupal\Core\Entity\EntityTypeInterface;
/**
* @file
* field validation.
*/
function field_validation_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
$ruleset_name = $entity_type
->id() . '_' . $bundle;
$ruleset = \Drupal::entityTypeManager()
->getStorage('field_validation_rule_set')
->load($ruleset_name);
if ($ruleset) {
foreach ($fields as $field) {
$field
->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $ruleset_name,
'rule_uuid' => '',
]);
}
/*
$rules = $ruleset->getFieldValidationRules();
foreach ($rules as $field_validation_rule) {
$field_name = $field_validation_rule->getFieldName();
if(isset($fields[$field_name])){
$fields[$field_name]->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $ruleset_name,
'rule_uuid' =>$field_validation_rule->getUuid(),
]);
}
}
*/
}
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function field_validation_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
$rulesets = \Drupal::entityTypeManager()
->getStorage('field_validation_rule_set')
->loadByProperties([
'entity_type' => $entity_type
->id(),
]);
foreach ($rulesets as $ruleset_name => $ruleset) {
$rules = $ruleset
->getFieldValidationRules();
foreach ($rules as $field_validation_rule) {
$field_name = $field_validation_rule
->getFieldName();
if (isset($fields[$field_name])) {
$fields[$field_name]
->addConstraint('FieldValidationConstraint', [
'ruleset_name' => $ruleset_name,
'rule_uuid' => '',
]);
}
}
}
}
Functions
Name | Description |
---|---|
field_validation_entity_base_field_info_alter | Implements hook_entity_base_field_info_alter(). |
field_validation_entity_bundle_field_info_alter | @file field validation. |