View source
<?php
namespace Drupal\views_ui;
use Drupal\Core\Form\FormStateInterface;
class ViewDuplicateForm extends ViewFormBase {
protected function prepareEntity() {
}
public function form(array $form, FormStateInterface $form_state) {
parent::form($form, $form_state);
$form['#title'] = $this
->t('Duplicate of @label', [
'@label' => $this->entity
->label(),
]);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('View name'),
'#required' => TRUE,
'#size' => 32,
'#maxlength' => 255,
'#default_value' => $this
->t('Duplicate of @label', [
'@label' => $this->entity
->label(),
]),
];
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => 128,
'#machine_name' => [
'exists' => '\\Drupal\\views\\Views::getView',
'source' => [
'label',
],
],
'#default_value' => '',
'#description' => $this
->t('A unique machine-readable name for this View. It must only contain lowercase letters, numbers, and underscores.'),
];
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Duplicate'),
];
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity = $this->entity
->createDuplicate();
$this->entity
->set('label', $form_state
->getValue('label'));
$this->entity
->set('id', $form_state
->getValue('id'));
$this->entity
->save();
$form_state
->setRedirectUrl($this->entity
->toUrl('edit-form'));
}
}