Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to use data entered in form in a plugin for a custom process action

Solved by Anna View solution

Started by Anna 7 years ago · 2 replies · 1099 views
7 years ago

I have a form with a custom action that gets processed via a plugin. I want to send a customised email. However, I do not know how to access the values that are contained in the Data object…

My code looks like this:

PHP
    public function onFormProcessed(Event $event)
    {
        $form = $event['form'];
        $action = $event['action'];
        $params = $event['params'];

        switch ($action) {
            case 'emailafile': // my custom action
                $data = $form->Data;
                dump($data); exit;
                $to = '[email protected]';
                $from = '[email protected]';

                $subject = 'Test!';
                $content = 'mehr Test…';
                $filename = ROOT_DIR . 'path/to/file.pdf';

                $message = $this->grav['Email']->message($subject, $content, 'text/html')
                    ->setFrom($from)
                    ->setTo($to)
                    ->attach(\Swift_Attachment::fromPath($filename));

                $sent = $this->grav['Email']->send($message);
        }
    }

The emailing code works nicely, I only need to figure out how to insert the variables from the form. So this `dump($data)' above gives me the following output:

Data-dump|273x139
And the #items bit contains everything I need – but how do I access it in PHP? I have tried:

PHP
$data->items
$data['items']

and a few other wild guesses, but to no avail. Can somebody help me out here please? Maybe there's a plugin that handles form data where I could take a look?

Thank you for your time!

7 years ago Solution

Hey, I got this bit figured out! $form->value('fieldname') gets me whatever I need. 😃 I'm now stuck on something else (of course), but that shall have its own thread…

7 years ago

You can even access form values from forms that you have not created with YAML, but your own HTML forms. But you need to add soemthing with "data" to the value attribute. Check out how Grav (Form plugin) generates the input fields.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1138 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 62 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 136 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 110 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 130 7 months ago