Chris@0: wizardManager = $wizard_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('plugin.manager.views.wizard') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function prepareEntity() { Chris@0: // Do not prepare the entity while it is being added. Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function form(array $form, FormStateInterface $form_state) { Chris@0: $form['#attached']['library'][] = 'views_ui/views_ui.admin'; Chris@0: $form['#attributes']['class'] = ['views-admin']; Chris@0: Chris@0: $form['name'] = [ Chris@0: '#type' => 'fieldset', Chris@0: '#title' => t('View basic information'), Chris@0: '#attributes' => ['class' => ['fieldset-no-legend']], Chris@0: ]; Chris@0: Chris@0: $form['name']['label'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('View name'), Chris@0: '#required' => TRUE, Chris@0: '#size' => 32, Chris@0: '#default_value' => '', Chris@0: '#maxlength' => 255, Chris@0: ]; Chris@0: $form['name']['id'] = [ Chris@0: '#type' => 'machine_name', Chris@0: '#maxlength' => 128, Chris@0: '#machine_name' => [ Chris@0: 'exists' => '\Drupal\views\Views::getView', Chris@0: 'source' => ['name', 'label'], Chris@0: ], Chris@0: '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'), Chris@0: ]; Chris@0: Chris@0: $form['name']['description_enable'] = [ Chris@0: '#type' => 'checkbox', Chris@0: '#title' => $this->t('Description'), Chris@0: ]; Chris@0: $form['name']['description'] = [ Chris@0: '#type' => 'textfield', Chris@0: '#title' => $this->t('Provide description'), Chris@0: '#title_display' => 'invisible', Chris@0: '#size' => 64, Chris@0: '#default_value' => '', Chris@0: '#states' => [ Chris@0: 'visible' => [ Chris@0: ':input[name="description_enable"]' => ['checked' => TRUE], Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // Create a wrapper for the entire dynamic portion of the form. Everything Chris@0: // that can be updated by AJAX goes somewhere inside here. For example, this Chris@0: // is needed by "Show" dropdown (below); it changes the base table of the Chris@0: // view and therefore potentially requires all options on the form to be Chris@0: // dynamically updated. Chris@0: $form['displays'] = []; Chris@0: Chris@0: // Create the part of the form that allows the user to select the basic Chris@0: // properties of what the view will display. Chris@0: $form['displays']['show'] = [ Chris@0: '#type' => 'fieldset', Chris@0: '#title' => t('View settings'), Chris@0: '#tree' => TRUE, Chris@0: '#attributes' => ['class' => ['container-inline']], Chris@0: ]; Chris@0: Chris@0: // Create the "Show" dropdown, which allows the base table of the view to be Chris@0: // selected. Chris@0: $wizard_plugins = $this->wizardManager->getDefinitions(); Chris@0: $options = []; Chris@0: foreach ($wizard_plugins as $key => $wizard) { Chris@0: $options[$key] = $wizard['title']; Chris@0: } Chris@0: $form['displays']['show']['wizard_key'] = [ Chris@0: '#type' => 'select', Chris@0: '#title' => $this->t('Show'), Chris@0: '#options' => $options, Chris@0: ]; Chris@0: $show_form = &$form['displays']['show']; Chris@0: $default_value = \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'users'; Chris@0: $show_form['wizard_key']['#default_value'] = WizardPluginBase::getSelected($form_state, ['show', 'wizard_key'], $default_value, $show_form['wizard_key']); Chris@0: // Changing this dropdown updates the entire content of $form['displays'] via Chris@0: // AJAX. Chris@0: views_ui_add_ajax_trigger($show_form, 'wizard_key', ['displays']); Chris@0: Chris@0: // Build the rest of the form based on the currently selected wizard plugin. Chris@0: $wizard_key = $show_form['wizard_key']['#default_value']; Chris@0: $wizard_instance = $this->wizardManager->createInstance($wizard_key); Chris@0: $form = $wizard_instance->buildForm($form, $form_state); Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function actions(array $form, FormStateInterface $form_state) { Chris@0: $actions = parent::actions($form, $form_state); Chris@0: $actions['submit']['#value'] = $this->t('Save and edit'); Chris@0: // Remove EntityFormController::save() form the submission handlers. Chris@0: $actions['submit']['#submit'] = [[$this, 'submitForm']]; Chris@0: $actions['cancel'] = [ Chris@0: '#type' => 'submit', Chris@0: '#value' => $this->t('Cancel'), Chris@0: '#submit' => ['::cancel'], Chris@0: '#limit_validation_errors' => [], Chris@0: ]; Chris@0: return $actions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: $wizard_type = $form_state->getValue(['show', 'wizard_key']); Chris@0: $wizard_instance = $this->wizardManager->createInstance($wizard_type); Chris@0: $form_state->set('wizard', $wizard_instance->getPluginDefinition()); Chris@0: $form_state->set('wizard_instance', $wizard_instance); Chris@0: $errors = $wizard_instance->validateView($form, $form_state); Chris@0: Chris@0: foreach ($errors as $display_errors) { Chris@0: foreach ($display_errors as $name => $message) { Chris@0: $form_state->setErrorByName($name, $message); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: try { Chris@0: /** @var $wizard \Drupal\views\Plugin\views\wizard\WizardInterface */ Chris@0: $wizard = $form_state->get('wizard_instance'); Chris@0: $this->entity = $wizard->createView($form, $form_state); Chris@0: } Chris@0: // @todo Figure out whether it really makes sense to throw and catch exceptions on the wizard. Chris@0: catch (WizardException $e) { Chris@17: $this->messenger()->addError($e->getMessage()); Chris@0: $form_state->setRedirect('entity.view.collection'); Chris@0: return; Chris@0: } Chris@0: $this->entity->save(); Chris@17: $this->messenger()->addStatus($this->t('The view %name has been saved.', ['%name' => $form_state->getValue('label')])); Chris@18: $form_state->setRedirectUrl($this->entity->toUrl('edit-form')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler for the 'cancel' action. Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * The current state of the form. Chris@0: */ Chris@0: public function cancel(array $form, FormStateInterface $form_state) { Chris@0: $form_state->setRedirect('entity.view.collection'); Chris@0: } Chris@0: Chris@0: }