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
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Form controller for the Views duplicate form.
|
Chris@14
|
9 *
|
Chris@14
|
10 * @internal
|
Chris@0
|
11 */
|
Chris@0
|
12 class ViewDuplicateForm extends ViewFormBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * {@inheritdoc}
|
Chris@0
|
16 */
|
Chris@0
|
17 protected function prepareEntity() {
|
Chris@0
|
18 // Do not prepare the entity while it is being added.
|
Chris@0
|
19 }
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * {@inheritdoc}
|
Chris@0
|
23 */
|
Chris@0
|
24 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
25 parent::form($form, $form_state);
|
Chris@0
|
26
|
Chris@0
|
27 $form['#title'] = $this->t('Duplicate of @label', ['@label' => $this->entity->label()]);
|
Chris@0
|
28
|
Chris@0
|
29 $form['label'] = [
|
Chris@0
|
30 '#type' => 'textfield',
|
Chris@0
|
31 '#title' => $this->t('View name'),
|
Chris@0
|
32 '#required' => TRUE,
|
Chris@0
|
33 '#size' => 32,
|
Chris@0
|
34 '#maxlength' => 255,
|
Chris@0
|
35 '#default_value' => $this->t('Duplicate of @label', ['@label' => $this->entity->label()]),
|
Chris@0
|
36 ];
|
Chris@0
|
37 $form['id'] = [
|
Chris@0
|
38 '#type' => 'machine_name',
|
Chris@0
|
39 '#maxlength' => 128,
|
Chris@0
|
40 '#machine_name' => [
|
Chris@0
|
41 'exists' => '\Drupal\views\Views::getView',
|
Chris@0
|
42 'source' => ['label'],
|
Chris@0
|
43 ],
|
Chris@0
|
44 '#default_value' => '',
|
Chris@0
|
45 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
|
Chris@0
|
46 ];
|
Chris@0
|
47
|
Chris@0
|
48 return $form;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * {@inheritdoc}
|
Chris@0
|
53 */
|
Chris@0
|
54 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
55 $actions['submit'] = [
|
Chris@0
|
56 '#type' => 'submit',
|
Chris@0
|
57 '#value' => $this->t('Duplicate'),
|
Chris@0
|
58 ];
|
Chris@0
|
59 return $actions;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Form submission handler for the 'clone' action.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @param array $form
|
Chris@0
|
66 * An associative array containing the structure of the form.
|
Chris@0
|
67 * @param \Drupal\Core\Form\FormStateInterface $form_state
|
Chris@0
|
68 * A reference to a keyed array containing the current state of the form.
|
Chris@0
|
69 */
|
Chris@0
|
70 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
71 $this->entity = $this->entity->createDuplicate();
|
Chris@0
|
72 $this->entity->set('label', $form_state->getValue('label'));
|
Chris@0
|
73 $this->entity->set('id', $form_state->getValue('id'));
|
Chris@0
|
74 $this->entity->save();
|
Chris@0
|
75
|
Chris@0
|
76 // Redirect the user to the view admin form.
|
Chris@18
|
77 $form_state->setRedirectUrl($this->entity->toUrl('edit-form'));
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 }
|