class FormValidation in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/BusinessRulesReactsOn/FormValidation.php \Drupal\business_rules\Plugin\BusinessRulesReactsOn\FormValidation
Class FormBuild.
@package Drupal\business_rules\Plugin\BusinessRulesReactsOn
Plugin annotation
@BusinessRulesReactsOn(
id = "form_validation",
label = @Translation("Entity form validation"),
description = @Translation("Reacts when entity form is being validated."),
group = @Translation("Entity"),
eventName = "business_rules.form_validation",
hasTargetEntity = TRUE,
hasTargetBundle = TRUE,
priority = 1000,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\business_rules\Plugin\BusinessRulesReactsOnPlugin implements BusinessRulesReactsOnInterface
- class \Drupal\business_rules\Plugin\BusinessRulesReactsOn\FormValidation
- class \Drupal\business_rules\Plugin\BusinessRulesReactsOnPlugin implements BusinessRulesReactsOnInterface
Expanded class hierarchy of FormValidation
File
- src/
Plugin/ BusinessRulesReactsOn/ FormValidation.php, line 24
Namespace
Drupal\business_rules\Plugin\BusinessRulesReactsOnView source
class FormValidation extends BusinessRulesReactsOnPlugin {
/**
* Performs the BusinessRule form validation.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
public static function validateForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\business_rules\Util\BusinessRulesProcessor $processor */
/** @var \Drupal\business_rules\Events\BusinessRulesEvent $event */
/** @var \Drupal\Core\Entity\Entity $entity */
$event = $form_state
->get('business_rules_event');
$event
->setArgument('form_state', $form_state);
// Set a new entity to be the form_state entity values. The comparison will
// gonna be against this dummy entity.
$entityManager = \Drupal::entityTypeManager();
$entity = $form_state
->getFormObject()
->getEntity();
$entity_values = $form_state
->getValues();
$entity_values['type'] = $entity
->bundle();
// For some reason that I'm lazy to look for, the Comment entity does not
// use the key "type" to specify the bundle. It uses "comment_type" instead.
if ($entity
->getEntityTypeId() == 'comment') {
$entity_values['comment_type'] = $entity
->bundle();
}
try {
$new_entity = $entityManager
->getStorage($entity
->getEntityTypeId())
->create($entity_values);
} catch (\Exception $e) {
// The dummy entity could not be created. Let's try force field values.
$new_entity = clone $entity;
$array = $new_entity
->toArray();
foreach ($entity_values as $key => $entity_value) {
try {
if (in_array($key, array_keys($array))) {
$new_entity->{$key}
->setValue($entity_value);
}
} catch (\Exception $e) {
// Field not exists.
}
}
}
$event
->setArgument('entity', $new_entity);
$event
->setArgument('form_state', $form_state);
// The BusinessRulesProcessor process the items after form submission.
// To form validation we need to process the rule's items before it.
// In this case we need to call the processor right now.
$processor = \Drupal::getContainer()
->get('business_rules.processor');
$processor
->process($event);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BusinessRulesReactsOnPlugin:: |
protected | property | The business rules processor. | |
BusinessRulesReactsOnPlugin:: |
protected | property | The business rules util. | |
BusinessRulesReactsOnPlugin:: |
public | function |
Process the BusinessRule form for reactsOn plugins. Overrides BusinessRulesReactsOnInterface:: |
3 |
BusinessRulesReactsOnPlugin:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
FormValidation:: |
public static | function | Performs the BusinessRule form validation. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |