0

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.

2
  • What is it supposed to render? Or How?
    – Kevin
    Oct 11, 2022 at 12:24
  • Hello Kevin, I have just edited the post to add more details. Hope it anwsers your question.
    – Pedro
    Oct 11, 2022 at 13:16

1 Answer 1

0

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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.