From bfa46dd34db6f6d399b7dd80eea888df0b98310a Mon Sep 17 00:00:00 2001 From: anton-mellit Date: Sun, 12 Apr 2020 18:00:52 +0200 Subject: [PATCH] Any form field can be equipped with the following: ``` process: twig: some twig markup goes here ``` Additionally, form is equipped with a new action "process". When process action is called, all process:twig contents of all fields are evaluated the results are saved in the values of these fields. For instance ``` form: fields: - type: hidden name: path process: twig: "/blog/{{ grav.page.folder }}" process: - process: true - addComment: true ``` After the form is submitted, before saving the comment the field "path" is evaluated to /blog/{{ grav.page.folder }} --- form.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/form.php b/form.php index 792fb471..e07af12e 100644 --- a/form.php +++ b/form.php @@ -384,9 +384,11 @@ public function onFormProcessed(Event $event) $action = $event['action']; $params = $event['params']; - $this->process($form); - switch ($action) { + case 'process': + $this->process($form); + break; + case 'captcha': $captcha_config = $this->config->get('plugins.form.recaptcha'); @@ -857,6 +859,14 @@ protected function process($form) if (!empty($field['process']['fillWithCurrentDateTime'])) { $form->setData($field['name'], gmdate('D, d M Y H:i:s', time())); } + if (!empty($field['process']['twig'])) { + $twig = $this->grav['twig']; + $vars = [ + 'form' => $form + ]; + // Process with Twig + $form->setData($field['name'], $twig->processString($field['process']['twig'], $vars)); + } } }