Mercurial > hg > isophonics-drupal-site
comparison core/modules/views_ui/src/ViewAddForm.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\views_ui; | |
4 | |
5 use Drupal\Core\Form\FormStateInterface; | |
6 use Drupal\views\Plugin\views\wizard\WizardPluginBase; | |
7 use Drupal\views\Plugin\views\wizard\WizardException; | |
8 use Drupal\views\Plugin\ViewsPluginManager; | |
9 use Symfony\Component\DependencyInjection\ContainerInterface; | |
10 | |
11 /** | |
12 * Form controller for the Views edit form. | |
13 */ | |
14 class ViewAddForm extends ViewFormBase { | |
15 | |
16 /** | |
17 * The wizard plugin manager. | |
18 * | |
19 * @var \Drupal\views\Plugin\ViewsPluginManager | |
20 */ | |
21 protected $wizardManager; | |
22 | |
23 /** | |
24 * Constructs a new ViewEditForm object. | |
25 * | |
26 * @param \Drupal\views\Plugin\ViewsPluginManager $wizard_manager | |
27 * The wizard plugin manager. | |
28 */ | |
29 public function __construct(ViewsPluginManager $wizard_manager) { | |
30 $this->wizardManager = $wizard_manager; | |
31 } | |
32 | |
33 /** | |
34 * {@inheritdoc} | |
35 */ | |
36 public static function create(ContainerInterface $container) { | |
37 return new static( | |
38 $container->get('plugin.manager.views.wizard') | |
39 ); | |
40 } | |
41 | |
42 /** | |
43 * {@inheritdoc} | |
44 */ | |
45 protected function prepareEntity() { | |
46 // Do not prepare the entity while it is being added. | |
47 } | |
48 | |
49 /** | |
50 * {@inheritdoc} | |
51 */ | |
52 public function form(array $form, FormStateInterface $form_state) { | |
53 $form['#attached']['library'][] = 'views_ui/views_ui.admin'; | |
54 $form['#attributes']['class'] = ['views-admin']; | |
55 | |
56 $form['name'] = [ | |
57 '#type' => 'fieldset', | |
58 '#title' => t('View basic information'), | |
59 '#attributes' => ['class' => ['fieldset-no-legend']], | |
60 ]; | |
61 | |
62 $form['name']['label'] = [ | |
63 '#type' => 'textfield', | |
64 '#title' => $this->t('View name'), | |
65 '#required' => TRUE, | |
66 '#size' => 32, | |
67 '#default_value' => '', | |
68 '#maxlength' => 255, | |
69 ]; | |
70 $form['name']['id'] = [ | |
71 '#type' => 'machine_name', | |
72 '#maxlength' => 128, | |
73 '#machine_name' => [ | |
74 'exists' => '\Drupal\views\Views::getView', | |
75 'source' => ['name', 'label'], | |
76 ], | |
77 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'), | |
78 ]; | |
79 | |
80 $form['name']['description_enable'] = [ | |
81 '#type' => 'checkbox', | |
82 '#title' => $this->t('Description'), | |
83 ]; | |
84 $form['name']['description'] = [ | |
85 '#type' => 'textfield', | |
86 '#title' => $this->t('Provide description'), | |
87 '#title_display' => 'invisible', | |
88 '#size' => 64, | |
89 '#default_value' => '', | |
90 '#states' => [ | |
91 'visible' => [ | |
92 ':input[name="description_enable"]' => ['checked' => TRUE], | |
93 ], | |
94 ], | |
95 ]; | |
96 | |
97 // Create a wrapper for the entire dynamic portion of the form. Everything | |
98 // that can be updated by AJAX goes somewhere inside here. For example, this | |
99 // is needed by "Show" dropdown (below); it changes the base table of the | |
100 // view and therefore potentially requires all options on the form to be | |
101 // dynamically updated. | |
102 $form['displays'] = []; | |
103 | |
104 // Create the part of the form that allows the user to select the basic | |
105 // properties of what the view will display. | |
106 $form['displays']['show'] = [ | |
107 '#type' => 'fieldset', | |
108 '#title' => t('View settings'), | |
109 '#tree' => TRUE, | |
110 '#attributes' => ['class' => ['container-inline']], | |
111 ]; | |
112 | |
113 // Create the "Show" dropdown, which allows the base table of the view to be | |
114 // selected. | |
115 $wizard_plugins = $this->wizardManager->getDefinitions(); | |
116 $options = []; | |
117 foreach ($wizard_plugins as $key => $wizard) { | |
118 $options[$key] = $wizard['title']; | |
119 } | |
120 $form['displays']['show']['wizard_key'] = [ | |
121 '#type' => 'select', | |
122 '#title' => $this->t('Show'), | |
123 '#options' => $options, | |
124 ]; | |
125 $show_form = &$form['displays']['show']; | |
126 $default_value = \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'users'; | |
127 $show_form['wizard_key']['#default_value'] = WizardPluginBase::getSelected($form_state, ['show', 'wizard_key'], $default_value, $show_form['wizard_key']); | |
128 // Changing this dropdown updates the entire content of $form['displays'] via | |
129 // AJAX. | |
130 views_ui_add_ajax_trigger($show_form, 'wizard_key', ['displays']); | |
131 | |
132 // Build the rest of the form based on the currently selected wizard plugin. | |
133 $wizard_key = $show_form['wizard_key']['#default_value']; | |
134 $wizard_instance = $this->wizardManager->createInstance($wizard_key); | |
135 $form = $wizard_instance->buildForm($form, $form_state); | |
136 | |
137 return $form; | |
138 } | |
139 | |
140 /** | |
141 * {@inheritdoc} | |
142 */ | |
143 protected function actions(array $form, FormStateInterface $form_state) { | |
144 $actions = parent::actions($form, $form_state); | |
145 $actions['submit']['#value'] = $this->t('Save and edit'); | |
146 // Remove EntityFormController::save() form the submission handlers. | |
147 $actions['submit']['#submit'] = [[$this, 'submitForm']]; | |
148 $actions['cancel'] = [ | |
149 '#type' => 'submit', | |
150 '#value' => $this->t('Cancel'), | |
151 '#submit' => ['::cancel'], | |
152 '#limit_validation_errors' => [], | |
153 ]; | |
154 return $actions; | |
155 } | |
156 | |
157 /** | |
158 * {@inheritdoc} | |
159 */ | |
160 public function validateForm(array &$form, FormStateInterface $form_state) { | |
161 $wizard_type = $form_state->getValue(['show', 'wizard_key']); | |
162 $wizard_instance = $this->wizardManager->createInstance($wizard_type); | |
163 $form_state->set('wizard', $wizard_instance->getPluginDefinition()); | |
164 $form_state->set('wizard_instance', $wizard_instance); | |
165 $errors = $wizard_instance->validateView($form, $form_state); | |
166 | |
167 foreach ($errors as $display_errors) { | |
168 foreach ($display_errors as $name => $message) { | |
169 $form_state->setErrorByName($name, $message); | |
170 } | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * {@inheritdoc} | |
176 */ | |
177 public function submitForm(array &$form, FormStateInterface $form_state) { | |
178 try { | |
179 /** @var $wizard \Drupal\views\Plugin\views\wizard\WizardInterface */ | |
180 $wizard = $form_state->get('wizard_instance'); | |
181 $this->entity = $wizard->createView($form, $form_state); | |
182 } | |
183 // @todo Figure out whether it really makes sense to throw and catch exceptions on the wizard. | |
184 catch (WizardException $e) { | |
185 drupal_set_message($e->getMessage(), 'error'); | |
186 $form_state->setRedirect('entity.view.collection'); | |
187 return; | |
188 } | |
189 $this->entity->save(); | |
190 drupal_set_message($this->t('The view %name has been saved.', ['%name' => $form_state->getValue('label')])); | |
191 $form_state->setRedirectUrl($this->entity->urlInfo('edit-form')); | |
192 } | |
193 | |
194 /** | |
195 * Form submission handler for the 'cancel' action. | |
196 * | |
197 * @param array $form | |
198 * An associative array containing the structure of the form. | |
199 * @param \Drupal\Core\Form\FormStateInterface $form_state | |
200 * The current state of the form. | |
201 */ | |
202 public function cancel(array $form, FormStateInterface $form_state) { | |
203 $form_state->setRedirect('entity.view.collection'); | |
204 } | |
205 | |
206 } |