3 namespace Drupal\views_ui;
5 use Drupal\Core\Form\FormStateInterface;
8 * Form controller for the Views duplicate form.
10 class ViewDuplicateForm extends ViewFormBase {
15 protected function prepareEntity() {
16 // Do not prepare the entity while it is being added.
22 public function form(array $form, FormStateInterface $form_state) {
23 parent::form($form, $form_state);
25 $form['#title'] = $this->t('Duplicate of @label', ['@label' => $this->entity->label()]);
28 '#type' => 'textfield',
29 '#title' => $this->t('View name'),
33 '#default_value' => $this->t('Duplicate of @label', ['@label' => $this->entity->label()]),
36 '#type' => 'machine_name',
39 'exists' => '\Drupal\views\Views::getView',
40 'source' => ['label'],
42 '#default_value' => '',
43 '#description' => $this->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
52 protected function actions(array $form, FormStateInterface $form_state) {
53 $actions['submit'] = [
55 '#value' => $this->t('Duplicate'),
61 * Form submission handler for the 'clone' action.
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.
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();
74 // Redirect the user to the view admin form.
75 $form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));