class Callbacks

Same name in this branch
  1. 11.x core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php \Drupal\ajax_forms_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. 10 core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php \Drupal\ajax_forms_test\Callbacks
  6. 10 core/modules/system/tests/modules/form_test/src/Callbacks.php \Drupal\form_test\Callbacks

Simple class for testing methods as Form API callbacks.

Hierarchy

Expanded class hierarchy of Callbacks

5 files declare their use of Callbacks
ElementsTableSelectTest.php in core/modules/system/tests/src/Functional/Form/ElementsTableSelectTest.php
FormTestHooks.php in core/modules/system/tests/modules/form_test/src/Hook/FormTestHooks.php
FormTestTableSelectColspanForm.php in core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectColspanForm.php
FormTestTableSelectFormBase.php in core/modules/system/tests/modules/form_test/src/Form/FormTestTableSelectFormBase.php
FormTestValidateForm.php in core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php

File

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

Namespace

Drupal\form_test
View source
class Callbacks {
    use StringTranslationTrait;
    
    /**
     * Form element validation handler for 'name' in form_test_validate_form().
     */
    public function validateName(&$element, FormStateInterface $form_state) {
        $triggered = FALSE;
        if ($form_state->getValue('name') == 'element_validate') {
            // Alter the form element.
            $element['#value'] = '#value changed by #element_validate';
            // Alter the submitted value in $form_state.
            $form_state->setValueForElement($element, 'value changed by setValueForElement() in #element_validate');
            $triggered = TRUE;
        }
        if ($form_state->getValue('name') == 'element_validate_access') {
            $form_state->set('form_test_name', $form_state->getValue('name'));
            // Alter the form element.
            $element['#access'] = FALSE;
            $triggered = TRUE;
        }
        elseif ($form_state->has('form_test_name')) {
            // To simplify this test, just take over the element's value into $form_state.
            $form_state->setValueForElement($element, $form_state->get('form_test_name'));
            $triggered = TRUE;
        }
        if ($triggered) {
            // Output the element's value from $form_state.
            \Drupal::messenger()->addStatus($this->t('@label value: @value', [
                '@label' => $element['#title'],
                '@value' => $form_state->getValue('name'),
            ]));
            // Trigger a form validation error to see our changes.
            $form_state->setErrorByName('');
        }
    }
    
    /**
     * Create a header and options array. Helper function for callbacks.
     */
    public static function tableselectGetData() : array {
        $header = [
            'one' => t('One'),
            'two' => t('Two'),
            'three' => t('Three'),
            'four' => t('Four'),
        ];
        $options['row1'] = [
            'title' => [
                'data' => [
                    '#title' => t('row1'),
                ],
            ],
            'one' => 'row1col1',
            'two' => t('row1col2'),
            'three' => t('row1col3'),
            'four' => t('row1col4'),
        ];
        $options['row2'] = [
            'title' => [
                'data' => [
                    '#title' => t('row2'),
                ],
            ],
            'one' => 'row2col1',
            'two' => t('row2col2'),
            'three' => t('row2col3'),
            'four' => t('row2col4'),
        ];
        $options['row3'] = [
            'title' => [
                'data' => [
                    '#title' => t('row3'),
                ],
            ],
            'one' => 'row3col1',
            'two' => t('row3col2'),
            'three' => t('row3col3'),
            'four' => t('row3col4'),
        ];
        return [
            $header,
            $options,
        ];
    }
    
    /**
     * Submit callback that just lets the form rebuild.
     */
    public static function userRegisterFormRebuild(array $form, FormStateInterface $form_state) : void {
        \Drupal::messenger()->addStatus('Form rebuilt.');
        $form_state->setRebuild();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
Callbacks::tableselectGetData public static function Create a header and options array. Helper function for callbacks.
Callbacks::userRegisterFormRebuild public static function Submit callback that just lets the form rebuild.
Callbacks::validateName public function Form element validation handler for 'name' in form_test_validate_form().
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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