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 :
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.