Score:0

After rendering entity form how can be modified?

br flag

I rendering a custom entity form display with the following code:

$form = $this->entityFormBuilder->getForm($entity, 'array_delimiter');

This is perfect, as it shows what I set on the form display. However when I try to add a new submit callback function (i.e. $form['actions']['submit']['#submit'][] = 'custom_callback';) then this is not being called.
When I try to hide some form items, I lose their data. (i.e. $form['status']['#access'] = FALSE;).

But when I do all of this in a hook_form_FORM_ID_alter it does work as expected. The callback is being called while hidden form element values are not lost.

Is there a way to do this without an extra hook_form_FORM_ID_alter?

Kevin avatar
in flag
You can't add it there, bevause the builder functions don't know about your added code. Thus, you need the alter hook.
Jaypan avatar
de flag
This should be done in a form_alter hook.
br flag
No, I don't need form_alter, I just need to extend the form render object and set there everything and give that instead of the original when I set the render object for the `array_delimiter` display
Kevin avatar
in flag
You didn't mention that.
Score:1
cn flag

Forms are rendered in three stages:

  1. The structure of the form is built in FormInterface::buildForm. The result can be altered in a hook_form_alter().

  2. The form elements are processed. Only form elements which are processed can be submitted.

  3. The final render array of the form is rendered.

Despite the name the form builder not only builds the form elements, but also processes them, so that they are ready to be rendered. More complex form elements require this step because the final render structure is built in #process callbacks. But even simple form elements rendering fine without processing can't be submitted.

br flag
Aha, so the only workaround for this is to extend the original form class and instead of giving the original form object as render object, I give my extended form. Therefore the point 2 and 3 did not run yet when I changed some of the values of the form.
br flag
Yes, and it does work, because anyway I need to define the new form display's render object, I can extend the already existing one to add there the callback and extra fields, like this there's no random codes everywhere.
4uk4 avatar
cn flag
Yes, this is the most straightforward way. Set the extended form class as form handler in the entity type annotations.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.