Callbacks.php 1.34 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\ajax_forms_test\Callbacks.
6 7 8 9
 */

namespace Drupal\ajax_forms_test;

10 11 12
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\DataCommand;
use Drupal\Core\Ajax\HtmlCommand;
13
use Drupal\Core\Form\FormStateInterface;
14

15 16 17 18 19 20 21 22
/**
 * Simple object for testing methods as Ajax callbacks.
 */
class Callbacks {

  /**
   * Ajax callback triggered by select.
   */
23
  function selectCallback($form, FormStateInterface $form_state) {
24
    $response = new AjaxResponse();
25 26
    $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')));
27
    return $response;
28 29 30 31 32
  }

  /**
   * Ajax callback triggered by checkbox.
   */
33
  function checkboxCallback($form, FormStateInterface $form_state) {
34
    $response = new AjaxResponse();
35 36
    $response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state->getValue('checkbox')));
    $response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
37
    return $response;
38
  }
39 40 41 42 43 44 45 46

  /**
   * Ajax callback triggered by the checkbox in a #group.
   */
  function checkboxGroupCallback($form, FormStateInterface $form_state) {
    return $form['checkbox_in_group_wrapper'];
  }

47
}