Issue #3278377 by amateescu, s_leu, smustgrave, pooja saraah, rkoller, simohell: Creating a new workspace should also switch to it

merge-requests/7112/head
catch 2024-03-20 09:59:42 +00:00
parent e7159707c3
commit d84f2c9d69
5 changed files with 78 additions and 44 deletions

View File

@ -2,14 +2,11 @@
namespace Drupal\workspaces\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityConstraintViolationListInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\workspaces\WorkspaceManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -25,39 +22,17 @@ class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface
protected $entity;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
* The workspace manager.
*/
protected $messenger;
/**
* Constructs a new WorkspaceForm.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, MessengerInterface $messenger, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->messenger = $messenger;
}
protected WorkspaceManagerInterface $workspaceManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('messenger'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time')
);
$instance = parent::create($container);
$instance->workspaceManager = $container->get('workspaces.manager');
return $instance;
}
/**
@ -120,6 +95,27 @@ class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface
parent::flagViolations($violations, $form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state): array {
$actions = parent::actions($form, $form_state);
// When adding a new workspace, the default action should also activate it.
if ($this->entity->isNew()) {
$actions['submit']['#value'] = $this->t('Save and switch');
$actions['submit']['#submit'] = ['::submitForm', '::save', '::activate'];
$actions['save'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#submit' => ['::submitForm', '::save'],
];
}
return $actions;
}
/**
* {@inheritdoc}
*/
@ -134,11 +130,11 @@ class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface
if ($status == SAVED_UPDATED) {
$logger->notice('@type: updated %info.', $context);
$this->messenger->addMessage($this->t('Workspace %info has been updated.', $info));
$this->messenger()->addMessage($this->t('Workspace %info has been updated.', $info));
}
else {
$logger->notice('@type: added %info.', $context);
$this->messenger->addMessage($this->t('Workspace %info has been created.', $info));
$this->messenger()->addMessage($this->t('Workspace %info has been created.', $info));
}
if ($workspace->id()) {
@ -150,9 +146,24 @@ class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface
$form_state->setRedirectUrl($redirect);
}
else {
$this->messenger->addError($this->t('The workspace could not be saved.'));
$this->messenger()->addError($this->t('The workspace could not be saved.'));
$form_state->setRebuild();
}
}
/**
* Form submission handler for the 'submit' action.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function activate(array $form, FormStateInterface $form_state): void {
$this->workspaceManager->setActiveWorkspace($this->entity);
$this->messenger()->addMessage($this->t('%label is now the active workspace.', [
'%label' => $this->entity->label(),
]));
}
}

View File

@ -47,8 +47,7 @@ class WorkspaceBypassTest extends BrowserTestBase {
// Login as a limited-access user and create a workspace.
$this->drupalLogin($coach);
$bears = $this->createWorkspaceThroughUi('Bears', 'bears');
$this->switchToWorkspace($bears);
$bears = $this->createAndActivateWorkspaceThroughUi('Bears', 'bears');
// Now create a node in the Bears workspace, as the owner of that workspace.
$coach_bears_node = $this->createNodeThroughUi('Ditka Bears node', 'test');

View File

@ -50,9 +50,7 @@ class WorkspaceSwitcherTest extends BrowserTestBase {
* Tests switching workspace via the switcher block and admin page.
*/
public function testSwitchingWorkspaces() {
$vultures = $this->createWorkspaceThroughUi('Vultures', 'vultures');
$this->switchToWorkspace($vultures);
$this->createAndActivateWorkspaceThroughUi('Vultures', 'vultures');
$gravity = $this->createWorkspaceThroughUi('Gravity', 'gravity');
$this->drupalGet('/admin/config/workflow/workspaces/manage/' . $gravity->id() . '/activate');

View File

@ -80,15 +80,16 @@ class WorkspaceTest extends BrowserTestBase {
*/
public function testSpecialCharacters() {
$this->drupalLogin($this->editor1);
$page = $this->getSession()->getPage();
// Test a valid workspace name.
$this->createWorkspaceThroughUi('Workspace 1', 'a0_$()+-/');
$this->createAndActivateWorkspaceThroughUi('Workspace 1', 'workspace_1');
$this->assertSession()->elementTextContains('css', '.workspaces-toolbar-tab', 'Workspace 1');
// Test and invalid workspace name.
$this->drupalGet('/admin/config/workflow/workspaces/add');
$this->assertSession()->statusCodeEquals(200);
$page = $this->getSession()->getPage();
$page->fillField('label', 'workspace2');
$page->fillField('id', 'A!"£%^&*{}#~@?');
$page->findButton('Save')->click();
@ -251,11 +252,10 @@ class WorkspaceTest extends BrowserTestBase {
// Login and create a workspace.
$this->drupalLogin($this->rootUser);
$may_4 = $this->createWorkspaceThroughUi('May 4', 'may_4');
$this->switchToWorkspace($may_4);
$this->createAndActivateWorkspaceThroughUi('May 4', 'may_4');
// Create a node in the workspace.
$node = $this->createNodeThroughUi('A mayfly flies / In May or June', 'test');
$this->createNodeThroughUi('A mayfly flies / In May or June', 'test');
// Delete the workspace.
$this->drupalGet('/admin/config/workflow/workspaces/manage/may_4/delete');

View File

@ -46,6 +46,32 @@ trait WorkspaceTestUtilities {
return $entity;
}
/**
* Creates and activates a new Workspace through the UI.
*
* @param string $label
* The label of the workspace to create.
* @param string $id
* The ID of the workspace to create.
* @param string $parent
* (optional) The ID of the parent workspace. Defaults to '_none'.
*
* @return \Drupal\workspaces\WorkspaceInterface
* The workspace that was just created.
*/
protected function createAndActivateWorkspaceThroughUi(string $label, string $id, string $parent = '_none'): WorkspaceInterface {
$this->drupalGet('/admin/config/workflow/workspaces/add');
$this->submitForm([
'id' => $id,
'label' => $label,
'parent' => $parent,
], 'Save and switch');
$this->getSession()->getPage()->hasContent("$label ($id)");
return Workspace::load($id);
}
/**
* Creates a new Workspace through the UI.
*