By drupol on
Hello,
I am new to module development.
I like incrementing the counter using AJAX in the Drupal form, which works. I like to add a reset button to reset the counter, but I am not sure how.
<?php
namespace Drupal\demo\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class CounterForm extends FormBase {
public function getFormId() {
return 'demo_counter_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$count = $form_state->get('count') ?: 0;
$form['count'] = [
'#markup' => "<p>Total count: $count",
'#prefix' => '<div id="counter">',
'#suffix' => '</div>',
];
$form['increment'] = [
'#type' => 'submit',
'#value' => 'Increment',
'#ajax' => [
'callback' => [$this, 'ajaxRefresh'],
'wrapper' => 'counter',
],
];
return $form;
}
public function ajaxRefresh(array $form, FormStateInterface $form_state) {
return $form['count'];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$count = $form_state->get('count') ?: 0;
$form_state->set('count', ++$count);
$form_state->setRebuild();
}
}
So far incrementing the counter is working.
To reset the counter,
$form['reset'] = [
'#type' => 'submit',
'#value' => 'reset',
'#ajax' => [
'callback' => [$this, 'ajaxReset'],
'wrapper' => 'counter',
],
];
public function ajaxReset(array $form, FormStateInterface $form_state) {
$count = 0;
$form_state->set('count', $count);
return $form['count'];
}The reset button is now also incrementing the counter. Whenever I click the reset button, I would like the counter to reset to 0. How can I do it?
Comments
use Drupal\Core\Form\FormBase
Thank you! It works.
Thank you! It works.
Every time I click on Increment, I see the "Please wait " message. How to hide the message without using CSS? When I click on Increment, I just like to see the total count. The same applies to the reset button.
https://ibb.co/9T45Lb4
$form['increment'] = [
Hello,
Hello,
It's great to hear that you are exploring module development! Incrementing the counter using AJAX in a Drupal form is a good start. Now, let's work on adding a reset button to reset the counter.
To achieve this, you'll need to follow these steps:
Modify your Drupal form: First, add a new button element to your Drupal form. You can do this by using the '#type' attribute and setting it to 'button' in the form array.
Implement AJAX callback: Next, create a new AJAX callback function in your custom module. This function will be triggered when the reset button is clicked.
Reset the counter: Inside the AJAX callback function, reset the counter to its initial value. If you have stored the counter value in a variable, set it back to the starting point.
Update the form: After resetting the counter, you need to update the form element to display the new value. Use AJAX commands like 'replace', 'html', or 'data' to update the counter field with the reset value.
Here's a simplified example of the code:
// Inside your form builder function.
function my_module_my_form($form, &$form_state) {
// Your existing code to create the counter field.
$form['counter'] = array(
'#type' => 'textfield',
'#title' => t('Counter'),
'#default_value' => 0,
'#attributes' => array('readonly' => 'readonly'),
);
// Add the reset button.
$form['reset_button'] = array(
'#type' => 'button',
'#value' => t('Reset'),
'#ajax' => array(
'callback' => 'my_module_reset_counter_ajax_callback',
),
);
return $form;
}
// AJAX callback function to reset the counter.
function my_module_reset_counter_ajax_callback($form, &$form_state) {
// Reset the counter to its initial value (e.g., 0).
$form_state['values']['counter'] = 0;
// Return the updated counter field element.
return $form['counter'];
}
Remember to replace 'my_module' with the actual name of your custom module.
With these changes, when the user clicks the reset button, the counter will be reset to its starting value, and the form will display the updated counter without requiring a page refresh.
I hope this helps you in your module development journey! If you have any further questions, feel free to ask. Happy coding!