annotate core/modules/views_ui/src/ViewAddForm.php @ 19:fa3358dc1485 tip

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