Version 1
[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\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides an AJAX form block.
14  *
15  * @Block(
16  *   id = "ajax_forms_test_block",
17  *   admin_label = @Translation("AJAX test form"),
18  *   category = @Translation("Forms")
19  * )
20  */
21 class AjaxFormBlock extends BlockBase implements FormInterface, ContainerFactoryPluginInterface {
22
23   /**
24    * The form builder.
25    *
26    * @var \Drupal\Core\Form\FormBuilderInterface
27    */
28   protected $formBuilder;
29
30   /**
31    * Constructs a new AjaxFormBlock.
32    *
33    * @param array $configuration
34    *   A configuration array containing information about the plugin instance.
35    * @param string $plugin_id
36    *   The plugin ID for the plugin instance.
37    * @param mixed $plugin_definition
38    *   The plugin implementation definition.
39    * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
40    *   The form builder.
41    */
42   public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
43     parent::__construct($configuration, $plugin_id, $plugin_definition);
44     $this->formBuilder = $form_builder;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('form_builder')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function build() {
63     return $this->formBuilder->getForm($this);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getFormId() {
70     return 'ajax_forms_test_block';
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $form['test1'] = [
78       '#type' => 'select',
79       '#title' => $this->t('Test 1'),
80       '#required' => TRUE,
81       '#options' => [
82         'option1' => $this->t('Option 1'),
83         'option2' => $this->t('Option 2'),
84       ],
85       '#ajax' => [
86         'callback' => '::updateOptions',
87         'wrapper' => 'edit-test1-wrapper',
88       ],
89       '#prefix' => '<div id="edit-test1-wrapper">',
90       '#suffix' => '</div>',
91     ];
92     $form['actions'] = [
93       '#type' => 'actions',
94     ];
95     $form['actions']['submit'] = [
96       '#type' => 'submit',
97       '#value' => $this->t('Submit'),
98     ];
99     return $form;
100   }
101
102   /**
103    * Updates the options of a select list.
104    *
105    * @param array $form
106    *   An associative array containing the structure of the form.
107    * @param \Drupal\Core\Form\FormStateInterface $form_state
108    *   The current state of the form.
109    *
110    * @return array
111    *   The updated form element.
112    */
113   public function updateOptions(array $form, FormStateInterface $form_state) {
114     $form['test1']['#options']['option1'] = $this->t('Option 1!!!');
115     $form['test1']['#options'] += [
116       'option3' => $this->t('Option 3'),
117       'option4' => $this->t('Option 4'),
118     ];
119     return $form['test1'];
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function validateForm(array &$form, FormStateInterface $form_state) {
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function submitForm(array &$form, FormStateInterface $form_state) {
132     drupal_set_message('Submission successful.');
133   }
134
135 }