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