Score:0

I lose template suggestions when an ajax event is called

de flag

I'm generating theme suggestions based on the formdazzle module, I made my own variant because I needed another level of granularity of theme names and it works fine.

Well, in the alter hook I have the key #pre_render and the method to generate de suggestions:

Example

$form['#formdazzle'] = ['form_id' => $form_id];

 // Add our pre-render function to the end of the list.
 if (!isset($form['#pre_render'])) {
   $form['#pre_render'] = [];
 }
 $form['#pre_render'][] = [self::class, 'preRenderForm'];

When the form loads the suggestions work fine, now, I have a paragraph and the button to add more paragraph elements, when I click it Ajax is executed by default and the new element is created, but theme suggestions are not regenerated, and the new markup that is replaced is not the correct one(use drupal default suggestions). That is, I lose all the markup of the templates.

enter image description here

Doing tests, I notice that when the ajax event is executed, the pre_render function is not executed again and that is why the suggestions are not generated again.

Is there a way to make the pre_render rerun after an Ajax event?

Thanks.

4uk4 avatar
cn flag
In an Ajax callback only the dynamic parts of the form are rendered and replaced. This shouldn't affect formdazzle, though, because this is for input elements, also for replaced ones. But what is in the #pre_render callback, couldn't you place it somewhere else?
Alfred Armstrong avatar
cn flag
Theme suggestions should be relatively static, they shouldn't depend on dynamic properties that change in flight. You should be generating them via a hook_X_suggestions_alter, not via pre_render, if that's what you're doing.
chalo avatar
de flag
@AlfredArmstrong I understand what you mention, as I comment, in the pre_render what is done is to provide additional information so that the hook_X_suggestions_alter can add additional suggestions, in the end the suggestions come from these hooks.
Alfred Armstrong avatar
cn flag
Yes but pre render isn't being run every time so it's not going to work. hook_suggestions_alter has to be self-contained, ideally getting everything it needs from the parameters passed to it.
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.