Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / tests / modules / ajax_forms_test / src / Plugin / Block / AjaxFormBlock.php
1 <?php
2
3 namespace Drupal\ajax_forms_test\Plugin\Block;
4
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;
12
13 /**
14  * Provides an AJAX form block.
15  *
16  * @Block(
17  *   id = "ajax_forms_test_block",
18  *   admin_label = @Translation("AJAX test form"),
19  *   category = @Translation("Forms")
20  * )
21  */
22 class AjaxFormBlock extends BlockBase implements FormInterface, ContainerFactoryPluginInterface {
23
24   /**
25    * The form builder.
26    *
27    * @var \Drupal\Core\Form\FormBuilderInterface
28    */
29   protected $formBuilder;
30
31   /**
32    * The messenger.
33    *
34    * @var \Drupal\Core\Messenger\MessengerInterface
35    */
36   protected $messenger;
37
38   /**
39    * Constructs a new AjaxFormBlock.
40    *
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
48    *   The form builder.
49    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
50    *   The messenger.
51    */
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;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
62     return new static(
63       $configuration,
64       $plugin_id,
65       $plugin_definition,
66       $container->get('form_builder'),
67       $container->get('messenger')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function build() {
75     return $this->formBuilder->getForm($this);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getFormId() {
82     return 'ajax_forms_test_block';
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function buildForm(array $form, FormStateInterface $form_state) {
89     $form['test1'] = [
90       '#type' => 'select',
91       '#title' => $this->t('Test 1'),
92       '#required' => TRUE,
93       '#options' => [
94         'option1' => $this->t('Option 1'),
95         'option2' => $this->t('Option 2'),
96       ],
97       '#ajax' => [
98         'callback' => '::updateOptions',
99         'wrapper' => 'edit-test1-wrapper',
100       ],
101       '#prefix' => '<div id="edit-test1-wrapper">',
102       '#suffix' => '</div>',
103     ];
104     $form['actions'] = [
105       '#type' => 'actions',
106     ];
107     $form['actions']['submit'] = [
108       '#type' => 'submit',
109       '#value' => $this->t('Submit'),
110     ];
111     return $form;
112   }
113
114   /**
115    * Updates the options of a select list.
116    *
117    * @param array $form
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.
121    *
122    * @return array
123    *   The updated form element.
124    */
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'),
130     ];
131     return $form['test1'];
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function validateForm(array &$form, FormStateInterface $form_state) {
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   public function submitForm(array &$form, FormStateInterface $form_state) {
144     $this->messenger->addStatus('Submission successful.');
145   }
146
147 }