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