WorkspaceUnarchiveForm.php in Workspace 8
File
src/Form/WorkspaceUnarchiveForm.php
View source
<?php
namespace Drupal\workspace\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceUnarchiveForm extends ConfirmFormBase {
protected $workspaceManager;
protected $entityTypeManager;
protected $workspace;
public static function create(ContainerInterface $container) {
return new static($container
->get('workspace.manager'), $container
->get('entity_type.manager'));
}
public function __construct(WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->workspaceManager = $workspace_manager;
$this->entityTypeManager = $entity_type_manager;
}
public function getConfirmText() {
return $this
->t('Unarchive');
}
public function getDescription() {
return $this
->t('This action will unarchive the %workspace workspace.', [
'%workspace' => $this->workspace
->label(),
]);
}
public function getFormId() {
return 'workspace_unarchive';
}
public function buildForm(array $form, FormStateInterface $form_state, WorkspaceInterface $workspace = NULL) {
$this->workspace = $workspace;
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($this->workspace
->isPublished()) {
$form_state
->setErrorByName('', 'This workspace is not archived.');
}
}
public function getQuestion() {
return $this
->t('Are you sure you want to unarchive the %workspace workspace?', [
'%workspace' => $this->workspace
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.workspace.archived');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
try {
$this->workspace
->setPublished()
->save();
$form_state
->setRedirect('entity.workspace.collection');
} catch (\Exception $e) {
watchdog_exception('Workspace', $e);
$this
->messenger()
->addError($e
->getMessage());
}
$this
->messenger()
->addStatus($this
->t('The %workspace workspace has been successfully unarchived.', [
'%workspace' => $this->workspace
->label(),
]));
}
}