I have my own form that shows a list of products. It can be scrolled, but I would like a "Load More" button. My problem is that I can't store the current position, because in the ajax callback I can't use the $form_state->set, get functions. How can I increase the position in the ajax callback function called when the "load more" button is pressed?

Comments

jaypan’s picture

You can use $form_state->set() and ::get() in submission handlers, which are executed before the ajax callback.

Contact me to contract me for D7 -> D10/11 migrations.

realdream’s picture

Thank you for your reply. In the meantime, I managed to solve it in the same way. This confirmed that it is the right technique.
$form['next'] = [
'#type' => 'submit',
'#value' => $this->t('Next step'),
'#submit' => ['::nextSubmit'],
'#ajax' => [
'wrapper' => 'infinite-scroll-content',
'callback' => '::prompt',
],
];

public function nextSubmit(array $form, FormStateInterface $form_state) {
$cnt = (int)$form_state->get('cnt');
$form_state->set('cnt ', $cnt +1);
$form_state->setRebuild();
return;
}