Skip to content
Snippets Groups Projects
Commit 8584598b authored by Andrei Mateescu's avatar Andrei Mateescu Committed by Andrei Mateescu
Browse files

Issue #3397605 by amateescu: Support restoring and purging entity types...

Issue #3397605 by amateescu: Support restoring and purging entity types without a canonical link template
parent 66d62ca4
No related branches found
Tags 2.0.0-alpha6
No related merge requests found
......@@ -45,6 +45,16 @@ class EntityPurgeForm extends ContentEntityConfirmFormBase {
return $this->getRedirectUrl();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Trash operations are allowed in a workspace.
$form_state->set('workspace_safe', TRUE);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
......@@ -65,7 +75,7 @@ class EntityPurgeForm extends ContentEntityConfirmFormBase {
}
/**
* Returns the URL where the user should be redirected after deletion.
* Returns the URL where the user should be redirected after purging.
*
* @return \Drupal\Core\Url
* The redirect URL.
......
......@@ -4,6 +4,7 @@ namespace Drupal\trash\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides a generic base class for a content entity restore form.
......@@ -24,7 +25,9 @@ class EntityRestoreForm extends ContentEntityConfirmFormBase {
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->getRedirectUrl();
return Url::fromRoute('trash.admin_content_trash_entity_type', [
'entity_type_id' => $this->getEntity()->getEntityTypeId()],
);
}
/**
......@@ -34,6 +37,16 @@ class EntityRestoreForm extends ContentEntityConfirmFormBase {
return;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Trash operations are allowed in a workspace.
$form_state->set('workspace_safe', TRUE);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
......@@ -53,21 +66,22 @@ class EntityRestoreForm extends ContentEntityConfirmFormBase {
}
/**
* Returns the URL where the user should be redirected after deletion.
* Returns the URL where the user should be redirected after restoring.
*
* @return \Drupal\Core\Url
* The redirect URL.
*/
protected function getRedirectUrl() {
$entity = $this->getEntity();
if ($entity->hasLinkTemplate('collection')) {
// If available, return the collection URL.
return $entity->toUrl('collection');
}
else {
if ($entity->hasLinkTemplate('canonical')) {
// Otherwise fall back to the default link template.
return $entity->toUrl();
}
else {
return Url::fromRoute('trash.admin_content_trash_entity_type', [
'entity_type_id' => $entity->getEntityTypeId()],
);
}
}
}
......@@ -45,16 +45,11 @@ class RouteSubscriber extends RouteSubscriberBase {
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
if ($this->trashManager->isEntityTypeEnabled($entity_type)) {
$base_path = NULL;
if ($entity_type->hasLinkTemplate('canonical')) {
$base_path = $entity_type->getLinkTemplate('canonical');
}
elseif ($entity_type->hasLinkTemplate('edit-form')) {
$base_path = $entity_type->getLinkTemplate('edit-form');
}
if (!$base_path) {
continue;
else {
$base_path = "/admin/content/trash/$entity_type_id/{" . $entity_type_id . '}';
}
$parameters = [
......
......@@ -135,7 +135,7 @@ function _trash_generate_storage_class($original_class, $type = 'storage') {
*/
function trash_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
foreach ($entity_types as $entity_type) {
foreach ($entity_types as $entity_type_id => $entity_type) {
if (\Drupal::service('trash.manager')->isEntityTypeEnabled($entity_type)) {
if (!$entity_type->getFormClass('restore')) {
$entity_type->setFormClass('restore', EntityRestoreForm::class);
......@@ -144,12 +144,16 @@ function trash_entity_type_alter(array &$entity_types) {
$entity_type->setFormClass('purge', EntityPurgeForm::class);
}
// Provide link templates for the 'restore' and 'purge' routes.
if ($entity_type->hasLinkTemplate('canonical')) {
// Provide default route names for the restore path.
$base_path = $entity_type->getLinkTemplate('canonical');
$entity_type->setLinkTemplate('restore', $base_path . '/restore');
$entity_type->setLinkTemplate('purge', $base_path . '/purge');
}
else {
$base_path = "/admin/content/trash/$entity_type_id/{" . $entity_type_id . '}';
}
$entity_type->setLinkTemplate('restore', $base_path . '/restore');
$entity_type->setLinkTemplate('purge', $base_path . '/purge');
// Override node's access control handler, so we can skip the
// 'bypass node access' permission check if the node is deleted.
......
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