- Issue created by @moser
I've submitted a Drupal stackexchange question to replace this support request.
Question:
Is it possible to define the "source entity" for a webform when using entityTypeManager() to create() a webform programatically?
I am using a form that is rendered multiple times on a page, so I need each form to have a source entity so as to apply unique form_wrapper_id's, form_id's, etc. I am constructing and attaching the form using the following code:
function hook_entity_load($entities) {
foreach ($entities as $entity) {
// Build webform
$webform = \Drupal::entityTypeManager()->getStorage('webform')->create([
'id' => '<webform_id>',
'entity_type' => '<entity_type>', // Does not populate source entity
'entity_id' => '<entity_id>', // Does not populate source entity
]);
// Create render view to attach
$renderWebform = \Drupal::entityTypeManager()->getViewBuilder('webform')->view($webform);
// Attach to entity to render via twig template
$entity->webform = $renderWebform;
}
}
I have tried a few different variations on the above in an attempt to pass the source entity to the built form with no luck. Even without the source entity the forms do submit and the Ajax does work, but because they don't have source entities, all forms on the page get updated with replace commands and logic is applied indiscriminately. Wouldn't be a problem if the form was only rendered once, but becomes an issue since there are multiple of the same form on the page.
Is there a way to set the source entity when using entityTypeManager() to create() a webform in a hook?
Normally I would use a render array in a twig template to pass a source entity, like the code below
{{
{
'#type': 'webform',
'#webform': '<webform_id>',
'#entity_type': '<type>',
'#entity_id': <id>,
}
}}
The render array does set the source entity ( as the docs suggest → ), however my particular setup breaks the Ajax behavior of forms rendered using a render array. I am using...
This setup breaks Ajax submission for the render array form, but not forms generated via entityTypeManager() or loaded via a block. I've attempted to restore the appropriate uniqueness and Ajax behaviors by...
{{ drupal_entity('block','<block_id>') }}
- (also no source entity)For all of the above either the AJax fails or the forms conflict due to a lack of supplied source entity.
Inspecting the rendered HTML will also show that the multiple rendered forms do not have the appropriately "source entity" altered IDs and conditional logic. I believe the simplest solution is to find a way to pass the source entity, but perhaps there is some alternative solution that would get the Ajax behaviors resolved for a render array.
Active
6.3
Documentation
I've submitted a Drupal stackexchange question to replace this support request.