Skip to content
Snippets Groups Projects
Commit 5ac36d5d authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3050037 by baldwinlouie, yas, Xiaohua Guan: Add copy functionality to Server Template

parent d91aa22e
No related branches found
No related tags found
No related merge requests found
......@@ -48,3 +48,8 @@ entity.cloud_server_template.launch:
route_name: entity.cloud_server_template.launch
base_route: entity.cloud_server_template.canonical
title: 'Launch'
entity.cloud_server_template.copy:
route_name: entity.cloud_server_template.copy
base_route: entity.cloud_server_template.canonical
title: 'Copy'
......@@ -57,3 +57,15 @@ entity.cloud_server_template.launch:
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'launch server template'
entity.cloud_server_template.copy:
path: '/clouds/design/server_template/{cloud_context}/{cloud_server_template}/copy'
defaults:
_entity_form: cloud_server_template.copy
_title: 'Copy'
requirements:
# Use custom access that will check for cloud_context and the desired permission.
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit any cloud server template entities+edit own cloud server template entities'
......@@ -93,6 +93,15 @@ class CloudServerTemplateListBuilder extends CloudContentListBuilder {
];
}
}
if ($entity->hasLinkTemplate('copy')) {
if ($entity->access('update')) {
$operations['copy'] = [
'title' => t('Copy'),
'url' => $entity->toUrl('copy'),
'weight' => 100,
];
}
}
return $operations;
}
......
......@@ -32,6 +32,7 @@ use Drupal\user\UserInterface;
* "edit" = "Drupal\cloud\Form\CloudServerTemplateForm",
* "delete" = "Drupal\cloud\Form\CloudServerTemplateDeleteForm",
* "launch" = "Drupal\cloud\Form\CloudServerTemplateLaunchConfirm",
* "copy" = "Drupal\cloud\Form\CloudServerTemplateCopyConfirm",
* },
* "access" = "Drupal\cloud\Controller\CloudServerTemplateAccessControlHandler",
* "route_provider" = {
......@@ -66,6 +67,7 @@ use Drupal\user\UserInterface;
* "translation_revert" = "/clouds/design/server_template/{cloud_context}/{cloud_server_template}/revisions/{cloud_server_template_revision}/revert/{langcode}",
* "collection" = "/clouds/design/server_template/list/{cloud_context}",
* "launch" = "/clouds/design/server_template/{cloud_context}/{cloud_server_template}/launch",
* "copy" = "/clouds/design/server_template/{cloud_context}/{cloud_server_template}/copy"
* },
* bundle_entity_type = "cloud_server_template_type",
* field_ui_base_route = "entity.cloud_server_template_type.edit_form"
......
<?php
namespace Drupal\cloud\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form for copying a server template.
*/
class CloudServerTemplateCopyConfirm extends ContentEntityConfirmFormBase {
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* {@inheritdoc}
*/
public function __construct(EntityRepositoryInterface $entity_repository,
Messenger $messenger) {
parent::__construct($entity_repository);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
$entity = $this->entity;
return $this->t('Are you sure you want to copy %name?', [
'%name' => $entity->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Copy');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return '';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['copy_server_template_name'] = [
'#title' => $this->t('New Server template name'),
'#type' => 'textfield',
'#description' => $this->t('The new server template name to use.'),
'#default_value' => $this->t('Copy of @name',
[
'@name' => $this->entity->getName(),
]),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
$entity = $this->entity;
$url = $entity->toUrl('canonical');
$url->setRouteParameter('cloud_context', $entity->getCloudContext());
return $url;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$entity = $this->entity;
// Create the new server template.
$new_entity = $entity->createDuplicate();
$new_entity->setName($form_state->getValue('copy_server_template_name'));
$new_entity->validate();
$new_entity->save();
$this->messenger->addMessage(
$this->t('Server template copied.')
);
$form_state->setRedirectUrl($new_entity->toUrl('canonical'));
}
}
......@@ -308,6 +308,15 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
]));
// The revision is deleted.
$this->assertSession()->linkByHrefNotExists("server_template/$cloud_context/1/revisions/1/view");
// Test copy function.
$this->drupalGet('/clouds/design/server_template/list/' . $cloud_context);
$this->clickLink($add[0]['name[0][value]']);
$this->clickLink('Copy');
$copy_url = $this->getUrl();
$this->drupalPostForm($copy_url, [], 'Copy');
$this->assertText('Server template copied.');
$this->assertText('Copy of ' . $add[0]['name[0][value]']);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment