3 namespace Drupal\workspaces\Form;
5 use Drupal\Core\Entity\EntityConfirmFormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Messenger\MessengerInterface;
8 use Drupal\workspaces\WorkspaceAccessException;
9 use Drupal\workspaces\WorkspaceManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Handle activation of a workspace on administrative pages.
15 class WorkspaceActivateForm extends EntityConfirmFormBase implements WorkspaceFormInterface {
18 * The workspace entity.
20 * @var \Drupal\workspaces\WorkspaceInterface
25 * The workspace replication manager.
27 * @var \Drupal\workspaces\WorkspaceManagerInterface
29 protected $workspaceManager;
32 * The messenger service.
34 * @var \Drupal\Core\Messenger\MessengerInterface
39 * Constructs a new WorkspaceActivateForm.
41 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
42 * The workspace manager.
43 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
44 * The messenger service.
46 public function __construct(WorkspaceManagerInterface $workspace_manager, MessengerInterface $messenger) {
47 $this->workspaceManager = $workspace_manager;
48 $this->messenger = $messenger;
54 public static function create(ContainerInterface $container) {
56 $container->get('workspaces.manager'),
57 $container->get('messenger')
64 public function getQuestion() {
65 return $this->t('Would you like to activate the %workspace workspace?', ['%workspace' => $this->entity->label()]);
71 public function getDescription() {
72 return $this->t('Activate the %workspace workspace.', ['%workspace' => $this->entity->label()]);
78 public function getCancelUrl() {
79 return $this->entity->toUrl('collection');
85 public function buildForm(array $form, FormStateInterface $form_state) {
86 $form = parent::buildForm($form, $form_state);
88 // Content entity forms do not use the parent's #after_build callback.
89 unset($form['#after_build']);
97 public function actions(array $form, FormStateInterface $form_state) {
98 $actions = parent::actions($form, $form_state);
99 $actions['cancel']['#attributes']['class'][] = 'dialog-cancel';
106 public function submitForm(array &$form, FormStateInterface $form_state) {
108 $this->workspaceManager->setActiveWorkspace($this->entity);
109 $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $this->entity->label()]));
110 $form_state->setRedirect('<front>');
112 catch (WorkspaceAccessException $e) {
113 $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $this->entity->label()]));