3 namespace Drupal\workspaces\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Messenger\MessengerInterface;
9 use Drupal\workspaces\WorkspaceAccessException;
10 use Drupal\workspaces\WorkspaceManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a form that activates a different workspace.
16 class WorkspaceSwitcherForm extends FormBase implements WorkspaceFormInterface {
19 * The workspace manager.
21 * @var \Drupal\workspaces\WorkspaceManagerInterface
23 protected $workspaceManager;
26 * The workspace entity storage handler.
28 * @var \Drupal\Core\Entity\EntityStorageInterface
30 protected $workspaceStorage;
33 * The messenger service.
35 * @var \Drupal\Core\Messenger\MessengerInterface
40 * Constructs a new WorkspaceSwitcherForm.
42 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
43 * The workspace manager.
44 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
45 * The entity type manager.
46 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
47 * The messenger service.
49 public function __construct(WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
50 $this->workspaceManager = $workspace_manager;
51 $this->workspaceStorage = $entity_type_manager->getStorage('workspace');
52 $this->messenger = $messenger;
58 public static function create(ContainerInterface $container) {
60 $container->get('workspaces.manager'),
61 $container->get('entity_type.manager'),
62 $container->get('messenger')
69 public function getFormId() {
70 return 'workspace_switcher_form';
76 public function buildForm(array $form, FormStateInterface $form_state) {
77 $workspaces = $this->workspaceStorage->loadMultiple();
78 $workspace_labels = [];
79 foreach ($workspaces as $workspace) {
80 $workspace_labels[$workspace->id()] = $workspace->label();
83 $active_workspace = $this->workspaceManager->getActiveWorkspace();
84 unset($workspace_labels[$active_workspace->id()]);
88 '#title' => $this->t('Current workspace'),
89 '#markup' => $active_workspace->label(),
90 '#wrapper_attributes' => [
91 'class' => ['container-inline'],
95 $form['workspace_id'] = [
97 '#title' => $this->t('Select workspace'),
99 '#options' => $workspace_labels,
100 '#wrapper_attributes' => [
101 'class' => ['container-inline'],
107 '#value' => $this->t('Activate'),
116 public function submitForm(array &$form, FormStateInterface $form_state) {
117 $id = $form_state->getValue('workspace_id');
119 /** @var \Drupal\workspaces\WorkspaceInterface $workspace */
120 $workspace = $this->workspaceStorage->load($id);
123 $this->workspaceManager->setActiveWorkspace($workspace);
124 $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $workspace->label()]));
126 catch (WorkspaceAccessException $e) {
127 $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $workspace->label()]));