Callbacks.php in Drupal 9
File
core/modules/system/tests/modules/form_test/src/Callbacks.php
View source
<?php
namespace Drupal\form_test;
use Drupal\Core\Form\FormStateInterface;
class Callbacks {
public function validateName(&$element, FormStateInterface $form_state) {
$triggered = FALSE;
if ($form_state
->getValue('name') == 'element_validate') {
$element['#value'] = '#value changed by #element_validate';
$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'));
$element['#access'] = FALSE;
$triggered = TRUE;
}
elseif ($form_state
->has('form_test_name')) {
$form_state
->setValueForElement($element, $form_state
->get('form_test_name'));
$triggered = TRUE;
}
if ($triggered) {
\Drupal::messenger()
->addStatus(t('@label value: @value', [
'@label' => $element['#title'],
'@value' => $form_state
->getValue('name'),
]));
$form_state
->setErrorByName('');
}
}
}
Classes
Name |
Description |
Callbacks |
Simple class for testing methods as Form API callbacks. |