class WorkspaceActivateForm
Same name and namespace in other branches
- 11.x core/modules/workspaces/src/Form/WorkspaceActivateForm.php \Drupal\workspaces\Form\WorkspaceActivateForm
Handle activation of a workspace on administrative pages.
Hierarchy
- class \Drupal\Core\Form\FormBase extends \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Entity\EntityFormInterface implements \Drupal\Core\Form\FormBase
- class \Drupal\Core\Entity\EntityConfirmFormBase extends \Drupal\Core\Form\ConfirmFormInterface implements \Drupal\Core\Entity\EntityForm
- class \Drupal\workspaces\Form\WorkspaceActivateForm extends \Drupal\workspaces\Form\WorkspaceFormInterface implements \Drupal\Core\Entity\EntityConfirmFormBase
- class \Drupal\Core\Entity\EntityConfirmFormBase extends \Drupal\Core\Form\ConfirmFormInterface implements \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Entity\EntityFormInterface implements \Drupal\Core\Form\FormBase
Expanded class hierarchy of WorkspaceActivateForm
File
-
core/
modules/ workspaces/ src/ Form/ WorkspaceActivateForm.php, line 17
Namespace
Drupal\workspaces\FormView source
class WorkspaceActivateForm extends EntityConfirmFormBase implements WorkspaceFormInterface {
/**
* The workspace entity.
*
* @var \Drupal\workspaces\WorkspaceInterface
*/
protected $entity;
/**
* The workspace replication manager.
*
* @var \Drupal\workspaces\WorkspaceManagerInterface
*/
protected $workspaceManager;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new WorkspaceActivateForm.
*
* @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
* The workspace manager.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(WorkspaceManagerInterface $workspace_manager, MessengerInterface $messenger) {
$this->workspaceManager = $workspace_manager;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('workspaces.manager'), $container->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Would you like to activate the %workspace workspace?', [
'%workspace' => $this->entity
->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Activate the %workspace workspace.', [
'%workspace' => $this->entity
->label(),
]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->entity
->toUrl('collection');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
// Content entity forms do not use the parent's #after_build callback.
unset($form['#after_build']);
return $form;
}
/**
* {@inheritdoc}
*/
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['cancel']['#attributes']['class'][] = 'dialog-cancel';
return $actions;
}
/**
* {@inheritdoc}
*/
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(),
]));
} catch (WorkspaceAccessException $e) {
$this->messenger
->addError($this->t('You do not have access to activate the %workspace_label workspace.', [
'%workspace_label' => $this->entity
->label(),
]));
}
// Redirect to the workspace manage page by default.
if (!$this->getRequest()->query
->has('destination')) {
$form_state->setRedirectUrl($this->entity
->toUrl());
}
}
/**
* Checks access for the workspace activate form.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*/
public function checkAccess(RouteMatchInterface $route_match) {
/** @var \Drupal\workspaces\WorkspaceInterface $workspace */
$workspace = $route_match->getParameter('workspace');
$active_workspace = $this->workspaceManager
->getActiveWorkspace();
$access = AccessResult::allowedIf(!$active_workspace || $active_workspace && $active_workspace->id() != $workspace->id())
->addCacheableDependency($workspace);
return $access;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.