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