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.
14 class ViewAddForm extends ViewFormBase {
17 * The wizard plugin manager.
19 * @var \Drupal\views\Plugin\ViewsPluginManager
21 protected $wizardManager;
24 * Constructs a new ViewEditForm object.
26 * @param \Drupal\views\Plugin\ViewsPluginManager $wizard_manager
27 * The wizard plugin manager.
29 public function __construct(ViewsPluginManager $wizard_manager) {
30 $this->wizardManager = $wizard_manager;
36 public static function create(ContainerInterface $container) {
38 $container->get('plugin.manager.views.wizard')
45 protected function prepareEntity() {
46 // Do not prepare the entity while it is being added.
52 public function form(array $form, FormStateInterface $form_state) {
53 $form['#attached']['library'][] = 'views_ui/views_ui.admin';
54 $form['#attributes']['class'] = ['views-admin'];
57 '#type' => 'fieldset',
58 '#title' => t('View basic information'),
59 '#attributes' => ['class' => ['fieldset-no-legend']],
62 $form['name']['label'] = [
63 '#type' => 'textfield',
64 '#title' => $this->t('View name'),
67 '#default_value' => '',
70 $form['name']['id'] = [
71 '#type' => 'machine_name',
74 'exists' => '\Drupal\views\Views::getView',
75 'source' => ['name', 'label'],
77 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
80 $form['name']['description_enable'] = [
81 '#type' => 'checkbox',
82 '#title' => $this->t('Description'),
84 $form['name']['description'] = [
85 '#type' => 'textfield',
86 '#title' => $this->t('Provide description'),
87 '#title_display' => 'invisible',
89 '#default_value' => '',
92 ':input[name="description_enable"]' => ['checked' => TRUE],
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'] = [];
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'),
110 '#attributes' => ['class' => ['container-inline']],
113 // Create the "Show" dropdown, which allows the base table of the view to be
115 $wizard_plugins = $this->wizardManager->getDefinitions();
117 foreach ($wizard_plugins as $key => $wizard) {
118 $options[$key] = $wizard['title'];
120 $form['displays']['show']['wizard_key'] = [
122 '#title' => $this->t('Show'),
123 '#options' => $options,
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
130 views_ui_add_ajax_trigger($show_form, 'wizard_key', ['displays']);
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);
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'] = [
150 '#value' => $this->t('Cancel'),
151 '#submit' => ['::cancel'],
152 '#limit_validation_errors' => [],
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);
167 foreach ($errors as $display_errors) {
168 foreach ($display_errors as $name => $message) {
169 $form_state->setErrorByName($name, $message);
177 public function submitForm(array &$form, FormStateInterface $form_state) {
179 /** @var $wizard \Drupal\views\Plugin\views\wizard\WizardInterface */
180 $wizard = $form_state->get('wizard_instance');
181 $this->entity = $wizard->createView($form, $form_state);
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');
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'));
195 * Form submission handler for the 'cancel' action.
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.
202 public function cancel(array $form, FormStateInterface $form_state) {
203 $form_state->setRedirect('entity.view.collection');