3fc5570c53ca979c298e92228b66e6ad51175a07
[yaffs-website] / web / core / modules / workspaces / src / Form / WorkspaceSwitcherForm.php
1 <?php
2
3 namespace Drupal\workspaces\Form;
4
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;
12
13 /**
14  * Provides a form that activates a different workspace.
15  */
16 class WorkspaceSwitcherForm extends FormBase implements WorkspaceFormInterface {
17
18   /**
19    * The workspace manager.
20    *
21    * @var \Drupal\workspaces\WorkspaceManagerInterface
22    */
23   protected $workspaceManager;
24
25   /**
26    * The workspace entity storage handler.
27    *
28    * @var \Drupal\Core\Entity\EntityStorageInterface
29    */
30   protected $workspaceStorage;
31
32   /**
33    * The messenger service.
34    *
35    * @var \Drupal\Core\Messenger\MessengerInterface
36    */
37   protected $messenger;
38
39   /**
40    * Constructs a new WorkspaceSwitcherForm.
41    *
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.
48    */
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;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container) {
59     return new static(
60       $container->get('workspaces.manager'),
61       $container->get('entity_type.manager'),
62       $container->get('messenger')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getFormId() {
70     return 'workspace_switcher_form';
71   }
72
73   /**
74    * {@inheritdoc}
75    */
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();
81     }
82
83     $active_workspace = $this->workspaceManager->getActiveWorkspace();
84     unset($workspace_labels[$active_workspace->id()]);
85
86     $form['current'] = [
87       '#type' => 'item',
88       '#title' => $this->t('Current workspace'),
89       '#markup' => $active_workspace->label(),
90       '#wrapper_attributes' => [
91         'class' => ['container-inline'],
92       ],
93     ];
94
95     $form['workspace_id'] = [
96       '#type' => 'select',
97       '#title' => $this->t('Select workspace'),
98       '#required' => TRUE,
99       '#options' => $workspace_labels,
100       '#wrapper_attributes' => [
101         'class' => ['container-inline'],
102       ],
103     ];
104
105     $form['submit'] = [
106       '#type' => 'submit',
107       '#value' => $this->t('Activate'),
108     ];
109
110     return $form;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function submitForm(array &$form, FormStateInterface $form_state) {
117     $id = $form_state->getValue('workspace_id');
118
119     /** @var \Drupal\workspaces\WorkspaceInterface $workspace */
120     $workspace = $this->workspaceStorage->load($id);
121
122     try {
123       $this->workspaceManager->setActiveWorkspace($workspace);
124       $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $workspace->label()]));
125     }
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()]));
128     }
129   }
130
131 }