Add data in form handler
Created 10 months 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'];


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


ryanthompson  —  10 months 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:

Hope this helps!

aidanw  —  10 months 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!