3 namespace Drupal\views_ui;
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;
12 * Form controller for the Views edit form.
16 class ViewAddForm extends ViewFormBase {
19 * The wizard plugin manager.
21 * @var \Drupal\views\Plugin\ViewsPluginManager
23 protected $wizardManager;
26 * Constructs a new ViewEditForm object.
28 * @param \Drupal\views\Plugin\ViewsPluginManager $wizard_manager
29 * The wizard plugin manager.
31 public function __construct(ViewsPluginManager $wizard_manager) {
32 $this->wizardManager = $wizard_manager;
38 public static function create(ContainerInterface $container) {
40 $container->get('plugin.manager.views.wizard')
47 protected function prepareEntity() {
48 // Do not prepare the entity while it is being added.
54 public function form(array $form, FormStateInterface $form_state) {
55 $form['#attached']['library'][] = 'views_ui/views_ui.admin';
56 $form['#attributes']['class'] = ['views-admin'];
59 '#type' => 'fieldset',
60 '#title' => t('View basic information'),
61 '#attributes' => ['class' => ['fieldset-no-legend']],
64 $form['name']['label'] = [
65 '#type' => 'textfield',
66 '#title' => $this->t('View name'),
69 '#default_value' => '',
72 $form['name']['id'] = [
73 '#type' => 'machine_name',
76 'exists' => '\Drupal\views\Views::getView',
77 'source' => ['name', 'label'],
79 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
82 $form['name']['description_enable'] = [
83 '#type' => 'checkbox',
84 '#title' => $this->t('Description'),
86 $form['name']['description'] = [
87 '#type' => 'textfield',
88 '#title' => $this->t('Provide description'),
89 '#title_display' => 'invisible',
91 '#default_value' => '',
94 ':input[name="description_enable"]' => ['checked' => TRUE],
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'] = [];
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'),
112 '#attributes' => ['class' => ['container-inline']],
115 // Create the "Show" dropdown, which allows the base table of the view to be
117 $wizard_plugins = $this->wizardManager->getDefinitions();
119 foreach ($wizard_plugins as $key => $wizard) {
120 $options[$key] = $wizard['title'];
122 $form['displays']['show']['wizard_key'] = [
124 '#title' => $this->t('Show'),
125 '#options' => $options,
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
132 views_ui_add_ajax_trigger($show_form, 'wizard_key', ['displays']);
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);
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'] = [
152 '#value' => $this->t('Cancel'),
153 '#submit' => ['::cancel'],
154 '#limit_validation_errors' => [],
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);
169 foreach ($errors as $display_errors) {
170 foreach ($display_errors as $name => $message) {
171 $form_state->setErrorByName($name, $message);
179 public function submitForm(array &$form, FormStateInterface $form_state) {
181 /** @var $wizard \Drupal\views\Plugin\views\wizard\WizardInterface */
182 $wizard = $form_state->get('wizard_instance');
183 $this->entity = $wizard->createView($form, $form_state);
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');
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'));
197 * Form submission handler for the 'cancel' action.
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.
204 public function cancel(array $form, FormStateInterface $form_state) {
205 $form_state->setRedirect('entity.view.collection');