DummyAjaxWidget.php in Drupal 9
File
core/modules/image/tests/modules/image_module_test/src/Plugin/Field/FieldWidget/DummyAjaxWidget.php
View source
<?php
namespace Drupal\image_module_test\Plugin\Field\FieldWidget;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class DummyAjaxWidget extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element['select_widget'] = [
'#type' => 'select',
'#title' => $this
->t('Dummy select'),
'#options' => [
'pow' => 'Pow!',
'bam' => 'Bam!',
],
'#required' => TRUE,
'#ajax' => [
'callback' => static::class . '::dummyAjaxCallback',
'effect' => 'fade',
],
];
return $element;
}
public static function dummyAjaxCallback(array &$form, FormStateInterface $form_state) {
return new AjaxResponse();
}
}