3 namespace Drupal\views_ui;
5 use Drupal\Core\Form\FormStateInterface;
8 * Form controller for the Views duplicate form.
12 class ViewDuplicateForm extends ViewFormBase {
17 protected function prepareEntity() {
18 // Do not prepare the entity while it is being added.
24 public function form(array $form, FormStateInterface $form_state) {
25 parent::form($form, $form_state);
27 $form['#title'] = $this->t('Duplicate of @label', ['@label' => $this->entity->label()]);
30 '#type' => 'textfield',
31 '#title' => $this->t('View name'),
35 '#default_value' => $this->t('Duplicate of @label', ['@label' => $this->entity->label()]),
38 '#type' => 'machine_name',
41 'exists' => '\Drupal\views\Views::getView',
42 'source' => ['label'],
44 '#default_value' => '',
45 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
54 protected function actions(array $form, FormStateInterface $form_state) {
55 $actions['submit'] = [
57 '#value' => $this->t('Duplicate'),
63 * Form submission handler for the 'clone' action.
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.
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();
76 // Redirect the user to the view admin form.
77 $form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));