You are here

public function DisplayErrorMessageOnForm::execute in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/DisplayErrorMessageOnForm.php \Drupal\business_rules\Plugin\BusinessRulesAction\DisplayErrorMessageOnForm::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

src/Plugin/BusinessRulesAction/DisplayErrorMessageOnForm.php, line 49

Class

DisplayErrorMessageOnForm
Class ValidateFormField.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {
  if (!empty($event['form_state'])) {
    $field = $action
      ->getSettings('field');
    $message = nl2br($action
      ->getSettings('message'));
    $variables = $event
      ->getArgument('variables');
    $message = $this
      ->processVariables($message, $variables);
    $message = new FormattableMarkup($message, []);

    /** @var \Drupal\Core\Form\FormStateInterface $form_state */
    $form_state = $event
      ->getArgument('form_state');
    $form_state
      ->setErrorByName($field, $message);
    $result = [
      '#type' => 'markup',
      '#markup' => t('Error set on form. Field: %field, message: %message', [
        '%field' => $field,
        '%message' => $message,
      ]),
    ];
    return $result;
  }
}