Score:0

Impossible to use custom fields in twig linked to form

ml flag

I have a form in which I have two date fields and a filter button. This form is included in a custom view in which I have a table that displays data from the database. The date fields are here to filter the data that I display in the table. I use AJAX in my form with a callback function.

What I want to do is adding the data to my form with the callback function, and displaying it in my twig.

My problem is that the twig is not updated so I cannot display the data from my form after filtering.

Here is my form :

class StatistiqueForm extends FormBase {
    /**
    * {@inheritdoc}
    */
    public function getFormId()
    {
        return 'statistique_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $form['date_from'] = array (
            '#type' => 'date',
            '#title' => 'From',
            '#required' => FALSE,
        );

        $form['date_to'] = array (
            '#type' => 'date',
            '#title' => 'To',
            '#required' => FALSE,
        );

        $form['filter_button'] = array(
            '#type' => 'submit',
            '#value' => 'Filter',
            '#ajax' => [
                'callback' => [$this, 'submitDateFilter'],
                'event' => 'click',
            ],
        );

        $form['reset_button'] = array(
            '#type' => 'submit',    
            '#value' => 'Reset',
            '#attributes' => array(
                'onclick' => 'this.form.reset(); return false;',
            )
        );
        
        $form['#theme'] = 'form_statistique_form';
        $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        return $form;
    }

    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {}

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
    }

    public function submitDateFilter(array $form, FormStateInterface $form_state) {
        $form['#statistique_query_data'] = array(
            '#test' => 'test'
        );
    }
} 

And in the twig, when using {{ form['#statistique_query_data']['#test'] }}, nothing is displayed.

I also tried to add $form_state->setRebuild() and \Drupal::service("router.builder")->rebuild() in buildForm and AJAX callback submitDateFilter function, but it's not working better.

EDIT : here is a screen of the view :

View

What I want to do is when clicking on the "Filter" button, add fields to my form in the submitDateFilter function and be able to use these fields in the view. For example, with the above code, be able to display in my twig the field form['#statistique_query_data']['#test'] in one of the tables. But actually, when I click on the "Filter" button, nothing appears when using {{ form['#statistique_query_data']['#test'] }} in the twig.

Kevin avatar
in flag
What is it supposed to render? Or How?
Pedro avatar
ml flag
Hello Kevin, I have just edited the post to add more details. Hope it anwsers your question.
Score:0
ml flag

Found the solution :

In my twig, I added a selector on the element I want to modify :

<td><p id='element_to_modify'></p></td>

In my form, with the AJAX callback function linked to my "Filter" button, I used the AJAX ReplaceCommand to modify my element with any value, using id selector :

public function submitDateFilter(array $form, FormStateInterface $form_state) {
        $response = new AjaxResponse();
        $Selector = '#element_to_modify';
        $content = '<p id=\'element_to_modify\'>new value !</p>';
        $response->addCommand(new ReplaceCommand($Selector, $content, array()));
        return $response;
    }
I sit in a Tesla and translated this thread with Ai:

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.