Add data in form handler
Created 6 years ago by aidanw

I'm wanting to add some extra data to an entity in a form handler. I've got this working

public function handle(AddressFormBuilder $builder)
    {
        $customerField = [
            'field' => 'customer',
            'value' => $builder->getCustomer()
        ];

        $builder->addField($customerField['field'], [$customerField]);

        $builder->addFormField($this->fieldFactory->make($customerField, $builder->getFormStream(), $builder->getFormEntry()));

        $builder->setFormValue($customerField['field'], $customerField['value']);

        $_POST[$customerField['field']] = $customerField['value'];

        $builder->saveForm();
    }

Can't help thinking there must be an easier way to do this though, am I missing something?

Thanks

ryanthompson  —  6 years ago Best Answer

Hey there! If you are (looks like) setting a relational value.. as long as the field exists on the stream the setter/getter logic is done for you. So this code - is likely best done from the onSaving callback on your builder. Here is an example from the pages module of how I do something similar. Keep in mind these fields I am setting exist but no need to drag the field type into them: https://github.com/anomalylabs/pages-module/blob/2.4/src/Page/Form/PageFormBuilder.php#L58

Hope this helps!

aidanw  —  6 years ago

Yes it does, thanks a lot, just realised I asked a similar question a while ago and you pointed me the direction of callbacks then! Doh! https://pyrocms.com/forum/channels/streams-platform/signup-form-with-fixed-role