comparison core/modules/views_ui/src/ViewDuplicateForm.php @ 0:4c8ae668cc8c

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