class Callbacks

Same name in this branch
  1. 10 core/modules/system/tests/modules/form_test/src/Callbacks.php \Drupal\form_test\Callbacks
Same name in other branches
  1. 9 core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php \Drupal\ajax_forms_test\Callbacks
  2. 9 core/modules/system/tests/modules/form_test/src/Callbacks.php \Drupal\form_test\Callbacks
  3. 8.9.x core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php \Drupal\ajax_forms_test\Callbacks
  4. 8.9.x core/modules/system/tests/modules/form_test/src/Callbacks.php \Drupal\form_test\Callbacks
  5. 11.x core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php \Drupal\ajax_forms_test\Callbacks
  6. 11.x core/modules/system/tests/modules/form_test/src/Callbacks.php \Drupal\form_test\Callbacks

Simple object for testing methods as Ajax callbacks.

Hierarchy

Expanded class hierarchy of Callbacks

3 files declare their use of Callbacks
AjaxFormsTestAjaxElementsForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestAjaxElementsForm.php
AjaxFormsTestImageButtonForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestImageButtonForm.php
AjaxFormsTestSimpleForm.php in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php

File

core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php, line 13

Namespace

Drupal\ajax_forms_test
View source
class Callbacks {
    
    /**
     * Ajax callback triggered by select.
     */
    public function selectCallback($form, FormStateInterface $form_state) {
        $response = new AjaxResponse();
        $response->addCommand(new HtmlCommand('#ajax_selected_color', $form_state->getValue('select')));
        $response->addCommand(new DataCommand('#ajax_selected_color', 'form_state_value_select', $form_state->getValue('select')));
        return $response;
    }
    
    /**
     * Ajax callback triggered by date.
     */
    public function dateCallback($form, FormStateInterface $form_state) {
        $response = new AjaxResponse();
        $date = $form_state->getValue('date');
        $response->addCommand(new HtmlCommand('#ajax_date_value', sprintf('<div>%s</div>', $date)));
        $response->addCommand(new DataCommand('#ajax_date_value', 'form_state_value_date', $form_state->getValue('date')));
        return $response;
    }
    
    /**
     * Ajax callback triggered by datetime.
     */
    public function datetimeCallback($form, FormStateInterface $form_state) {
        $datetime = $form_state->getValue('datetime')['date'] . ' ' . $form_state->getValue('datetime')['time'];
        $response = new AjaxResponse();
        $response->addCommand(new HtmlCommand('#ajax_datetime_value', sprintf('<div>%s</div>', $datetime)));
        $response->addCommand(new DataCommand('#ajax_datetime_value', 'form_state_value_datetime', $datetime));
        return $response;
    }
    
    /**
     * Ajax callback triggered by checkbox.
     */
    public function checkboxCallback($form, FormStateInterface $form_state) {
        $response = new AjaxResponse();
        $response->addCommand(new HtmlCommand('#ajax_checkbox_value', $form_state->getValue('checkbox') ? 'checked' : 'unchecked'));
        $response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
        return $response;
    }
    
    /**
     * Ajax callback to confirm image button was submitted.
     */
    public function imageButtonCallback($form, FormStateInterface $form_state) {
        $response = new AjaxResponse();
        $response->addCommand(new HtmlCommand('#ajax_image_button_result', "<div id='ajax-1-more-div'>Something witty!</div>"));
        return $response;
    }
    
    /**
     * Ajax callback triggered by the checkbox in a #group.
     */
    public function checkboxGroupCallback($form, FormStateInterface $form_state) {
        return $form['checkbox_in_group_wrapper'];
    }

}

Members

Title Sort descending Modifiers Object type Summary
Callbacks::checkboxCallback public function Ajax callback triggered by checkbox.
Callbacks::checkboxGroupCallback public function Ajax callback triggered by the checkbox in a #group.
Callbacks::dateCallback public function Ajax callback triggered by date.
Callbacks::datetimeCallback public function Ajax callback triggered by datetime.
Callbacks::imageButtonCallback public function Ajax callback to confirm image button was submitted.
Callbacks::selectCallback public function Ajax callback triggered by select.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.