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