You are here

ajax_forms_test.module in Zircon Profile 8

Simpletest mock module for Ajax forms testing.

File

core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
View source
<?php

/**
 * @file
 * Simpletest mock module for Ajax forms testing.
 */
use Drupal\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormStateInterface;

/**
 * Ajax form callback: Selects 'after'.
 */
function ajax_forms_test_advanced_commands_after_callback($form, FormStateInterface $form_state) {
  $selector = '#after_div';
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\AfterCommand($selector, "This will be placed after"));
  return $response;
}

/**
 * Ajax form callback: Selects 'alert'.
 */
function ajax_forms_test_advanced_commands_alert_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\AlertCommand('Alert'));
  return $response;
}

/**
 * Ajax form callback: Selects 'append'.
 */
function ajax_forms_test_advanced_commands_append_callback($form, FormStateInterface $form_state) {
  $selector = '#append_div';
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\AppendCommand($selector, "Appended text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'before'.
 */
function ajax_forms_test_advanced_commands_before_callback($form, FormStateInterface $form_state) {
  $selector = '#before_div';
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\BeforeCommand($selector, "Before text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'changed'.
 */
function ajax_forms_test_advanced_commands_changed_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\ChangedCommand('#changed_div'));
  return $response;
}

/**
 * Ajax form callback: Selects 'changed' with asterisk marking inner div.
 */
function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\ChangedCommand('#changed_div', '#changed_div_mark_this'));
  return $response;
}

/**
 * Ajax form callback: Selects 'css'.
 */
function ajax_forms_test_advanced_commands_css_callback($form, FormStateInterface $form_state) {
  $selector = '#css_div';
  $color = 'blue';
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\CssCommand($selector, array(
    'background-color' => $color,
  )));
  return $response;
}

/**
 * Ajax form callback: Selects 'data'.
 */
function ajax_forms_test_advanced_commands_data_callback($form, FormStateInterface $form_state) {
  $selector = '#data_div';
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\DataCommand($selector, 'testkey', 'testvalue'));
  return $response;
}

/**
 * Ajax form callback: Selects 'invoke'.
 */
function ajax_forms_test_advanced_commands_invoke_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\InvokeCommand('#invoke_div', 'addClass', array(
    'error',
  )));
  return $response;
}

/**
 * Ajax form callback: Selects 'html'.
 */
function ajax_forms_test_advanced_commands_html_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\HtmlCommand('#html_div', 'replacement text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'insert'.
 */
function ajax_forms_test_advanced_commands_insert_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\InsertCommand('#insert_div', 'insert replacement text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'prepend'.
 */
function ajax_forms_test_advanced_commands_prepend_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\PrependCommand('#prepend_div', "prepended text"));
  return $response;
}

/**
 * Ajax form callback: Selects 'remove'.
 */
function ajax_forms_test_advanced_commands_remove_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\RemoveCommand('#remove_text'));
  return $response;
}

/**
 * Ajax form callback: Selects 'restripe'.
 */
function ajax_forms_test_advanced_commands_restripe_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\RestripeCommand('#restripe_table'));
  return $response;
}

/**
 * Ajax form callback: Selects 'settings'.
 */
function ajax_forms_test_advanced_commands_settings_callback($form, FormStateInterface $form_state) {
  $setting['ajax_forms_test']['foo'] = 42;
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\SettingsCommand($setting));
  return $response;
}

/**
 * Ajax callback for 'add_css'.
 */
function ajax_forms_test_advanced_commands_add_css_callback($form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response
    ->addCommand(new Ajax\AddCssCommand('my/file.css'));
  return $response;
}

/**
 * Ajax form callback: Selects the 'drivertext' element of the validation form.
 */
function ajax_forms_test_validation_form_callback($form, FormStateInterface $form_state) {
  drupal_set_message("ajax_forms_test_validation_form_callback invoked");
  drupal_set_message(t("Callback: drivertext=%drivertext, spare_required_field=%spare_required_field", array(
    '%drivertext' => $form_state
      ->getValue('drivertext'),
    '%spare_required_field' => $form_state
      ->getValue('spare_required_field'),
  )));
  return [
    '#markup' => '<div id="message_area">ajax_forms_test_validation_form_callback at ' . date('c') . '</div>',
  ];
}

/**
 * Ajax form callback: Selects the 'drivernumber' element of the validation form.
 */
function ajax_forms_test_validation_number_form_callback($form, FormStateInterface $form_state) {
  drupal_set_message("ajax_forms_test_validation_number_form_callback invoked");
  drupal_set_message(t("Callback: drivernumber=%drivernumber, spare_required_field=%spare_required_field", array(
    '%drivernumber' => $form_state
      ->getValue('drivernumber'),
    '%spare_required_field' => $form_state
      ->getValue('spare_required_field'),
  )));
  return [
    '#markup' => '<div id="message_area_number">ajax_forms_test_validation_number_form_callback at ' . date('c') . '</div>',
  ];
}

/**
 * AJAX form callback: Selects for the ajax_forms_test_lazy_load_form() form.
 */
function ajax_forms_test_lazy_load_form_ajax($form, FormStateInterface $form_state) {
  $build = [
    '#markup' => 'new content',
  ];
  if ($form_state
    ->getValue('add_files')) {
    $build['#attached']['library'][] = 'system/admin';
    $build['#attached']['library'][] = 'system/drupal.system';
    $build['#attached']['drupalSettings']['ajax_forms_test_lazy_load_form_submit'] = 'executed';
  }
  return $build;
}

Functions

Namesort descending Description
ajax_forms_test_advanced_commands_add_css_callback Ajax callback for 'add_css'.
ajax_forms_test_advanced_commands_after_callback Ajax form callback: Selects 'after'.
ajax_forms_test_advanced_commands_alert_callback Ajax form callback: Selects 'alert'.
ajax_forms_test_advanced_commands_append_callback Ajax form callback: Selects 'append'.
ajax_forms_test_advanced_commands_before_callback Ajax form callback: Selects 'before'.
ajax_forms_test_advanced_commands_changed_asterisk_callback Ajax form callback: Selects 'changed' with asterisk marking inner div.
ajax_forms_test_advanced_commands_changed_callback Ajax form callback: Selects 'changed'.
ajax_forms_test_advanced_commands_css_callback Ajax form callback: Selects 'css'.
ajax_forms_test_advanced_commands_data_callback Ajax form callback: Selects 'data'.
ajax_forms_test_advanced_commands_html_callback Ajax form callback: Selects 'html'.
ajax_forms_test_advanced_commands_insert_callback Ajax form callback: Selects 'insert'.
ajax_forms_test_advanced_commands_invoke_callback Ajax form callback: Selects 'invoke'.
ajax_forms_test_advanced_commands_prepend_callback Ajax form callback: Selects 'prepend'.
ajax_forms_test_advanced_commands_remove_callback Ajax form callback: Selects 'remove'.
ajax_forms_test_advanced_commands_restripe_callback Ajax form callback: Selects 'restripe'.
ajax_forms_test_advanced_commands_settings_callback Ajax form callback: Selects 'settings'.
ajax_forms_test_lazy_load_form_ajax AJAX form callback: Selects for the ajax_forms_test_lazy_load_form() form.
ajax_forms_test_validation_form_callback Ajax form callback: Selects the 'drivertext' element of the validation form.
ajax_forms_test_validation_number_form_callback Ajax form callback: Selects the 'drivernumber' element of the validation form.