WorkspaceActivateForm.php in Workspace 8.2
File
src/Form/WorkspaceActivateForm.php
View source
<?php
namespace Drupal\workspace\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\workspace\WorkspaceAccessException;
use Drupal\workspace\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceActivateForm extends EntityConfirmFormBase {
protected $entity;
protected $workspaceManager;
protected $messenger;
public function __construct(WorkspaceManagerInterface $workspace_manager, MessengerInterface $messenger) {
$this->workspaceManager = $workspace_manager;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('workspace.manager'), $container
->get('messenger'));
}
public function getQuestion() {
return $this
->t('Would you like to activate the %workspace workspace?', [
'%workspace' => $this->entity
->label(),
]);
}
public function getDescription() {
return $this
->t('Activate the %workspace workspace.', [
'%workspace' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return $this->entity
->toUrl('collection');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
unset($form['#after_build']);
return $form;
}
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['cancel']['#attributes']['class'][] = 'dialog-cancel';
return $actions;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
try {
$this->workspaceManager
->setActiveWorkspace($this->entity);
$this->messenger
->addMessage($this
->t('%workspace_label is now the active workspace.', [
'%workspace_label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
} catch (WorkspaceAccessException $e) {
$this->messenger
->addError($this
->t('You do not have access to activate the %workspace_label workspace.', [
'%workspace_label' => $this->entity
->label(),
]));
}
}
}