3 namespace Drupal\ajax_forms_test\Plugin\Block;
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Form\FormBuilderInterface;
7 use Drupal\Core\Form\FormInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Messenger\MessengerInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides an AJAX form block.
17 * id = "ajax_forms_test_block",
18 * admin_label = @Translation("AJAX test form"),
19 * category = @Translation("Forms")
22 class AjaxFormBlock extends BlockBase implements FormInterface, ContainerFactoryPluginInterface {
27 * @var \Drupal\Core\Form\FormBuilderInterface
29 protected $formBuilder;
34 * @var \Drupal\Core\Messenger\MessengerInterface
39 * Constructs a new AjaxFormBlock.
41 * @param array $configuration
42 * A configuration array containing information about the plugin instance.
43 * @param string $plugin_id
44 * The plugin ID for the plugin instance.
45 * @param mixed $plugin_definition
46 * The plugin implementation definition.
47 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
49 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
52 public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder, MessengerInterface $messenger) {
53 parent::__construct($configuration, $plugin_id, $plugin_definition);
54 $this->formBuilder = $form_builder;
55 $this->messenger = $messenger;
61 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
66 $container->get('form_builder'),
67 $container->get('messenger')
74 public function build() {
75 return $this->formBuilder->getForm($this);
81 public function getFormId() {
82 return 'ajax_forms_test_block';
88 public function buildForm(array $form, FormStateInterface $form_state) {
91 '#title' => $this->t('Test 1'),
94 'option1' => $this->t('Option 1'),
95 'option2' => $this->t('Option 2'),
98 'callback' => '::updateOptions',
99 'wrapper' => 'edit-test1-wrapper',
101 '#prefix' => '<div id="edit-test1-wrapper">',
102 '#suffix' => '</div>',
105 '#type' => 'actions',
107 $form['actions']['submit'] = [
109 '#value' => $this->t('Submit'),
115 * Updates the options of a select list.
118 * An associative array containing the structure of the form.
119 * @param \Drupal\Core\Form\FormStateInterface $form_state
120 * The current state of the form.
123 * The updated form element.
125 public function updateOptions(array $form, FormStateInterface $form_state) {
126 $form['test1']['#options']['option1'] = $this->t('Option 1!!!');
127 $form['test1']['#options'] += [
128 'option3' => $this->t('Option 3'),
129 'option4' => $this->t('Option 4'),
131 return $form['test1'];
137 public function validateForm(array &$form, FormStateInterface $form_state) {
143 public function submitForm(array &$form, FormStateInterface $form_state) {
144 $this->messenger->addStatus('Submission successful.');