Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / workspaces / src / Form / WorkspaceActivateForm.php
1 <?php
2
3 namespace Drupal\workspaces\Form;
4
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;
11
12 /**
13  * Handle activation of a workspace on administrative pages.
14  */
15 class WorkspaceActivateForm extends EntityConfirmFormBase implements WorkspaceFormInterface {
16
17   /**
18    * The workspace entity.
19    *
20    * @var \Drupal\workspaces\WorkspaceInterface
21    */
22   protected $entity;
23
24   /**
25    * The workspace replication manager.
26    *
27    * @var \Drupal\workspaces\WorkspaceManagerInterface
28    */
29   protected $workspaceManager;
30
31   /**
32    * The messenger service.
33    *
34    * @var \Drupal\Core\Messenger\MessengerInterface
35    */
36   protected $messenger;
37
38   /**
39    * Constructs a new WorkspaceActivateForm.
40    *
41    * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
42    *   The workspace manager.
43    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
44    *   The messenger service.
45    */
46   public function __construct(WorkspaceManagerInterface $workspace_manager, MessengerInterface $messenger) {
47     $this->workspaceManager = $workspace_manager;
48     $this->messenger = $messenger;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
56       $container->get('workspaces.manager'),
57       $container->get('messenger')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getQuestion() {
65     return $this->t('Would you like to activate the %workspace workspace?', ['%workspace' => $this->entity->label()]);
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getDescription() {
72     return $this->t('Activate the %workspace workspace.', ['%workspace' => $this->entity->label()]);
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getCancelUrl() {
79     return $this->entity->toUrl('collection');
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function buildForm(array $form, FormStateInterface $form_state) {
86     $form = parent::buildForm($form, $form_state);
87
88     // Content entity forms do not use the parent's #after_build callback.
89     unset($form['#after_build']);
90
91     return $form;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function actions(array $form, FormStateInterface $form_state) {
98     $actions = parent::actions($form, $form_state);
99     $actions['cancel']['#attributes']['class'][] = 'dialog-cancel';
100     return $actions;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function submitForm(array &$form, FormStateInterface $form_state) {
107     try {
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>');
111     }
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()]));
114     }
115   }
116
117 }