diff options
author | catch <6915-catch@users.noreply.drupalcode.org> | 2025-04-19 07:58:12 +0100 |
---|---|---|
committer | catch <6915-catch@users.noreply.drupalcode.org> | 2025-04-19 07:58:12 +0100 |
commit | 8a10f82ffc93f3fcf5a6d95a65cebe40f9c10011 (patch) | |
tree | 9bea252838fdf450713b42023b358aeaa00b96ef /core | |
parent | b4fffd14b546fc93be641050c29f8df80f83cf46 (diff) | |
download | drupal-8a10f82ffc93f3fcf5a6d95a65cebe40f9c10011.tar.gz drupal-8a10f82ffc93f3fcf5a6d95a65cebe40f9c10011.zip |
Issue #3147148 by DarKFlameS, amateescu, mandclu, smustgrave: Media library form can only be submitted in the default workspace
Diffstat (limited to 'core')
3 files changed, 71 insertions, 2 deletions
diff --git a/core/modules/media_library/src/Form/AddFormBase.php b/core/modules/media_library/src/Form/AddFormBase.php index 05f2596c032..869c3842134 100644 --- a/core/modules/media_library/src/Form/AddFormBase.php +++ b/core/modules/media_library/src/Form/AddFormBase.php @@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\BaseFormIdInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\WorkspaceSafeFormInterface; use Drupal\Core\Render\Element; use Drupal\Core\Security\TrustedCallbackInterface; use Drupal\Core\Url; @@ -27,7 +28,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a base class for creating media items from within the media library. */ -abstract class AddFormBase extends FormBase implements BaseFormIdInterface, TrustedCallbackInterface { +abstract class AddFormBase extends FormBase implements BaseFormIdInterface, TrustedCallbackInterface, WorkspaceSafeFormInterface { /** * The entity type manager. diff --git a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php index e6b8174c104..2e233cab5e4 100644 --- a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php +++ b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Ajax\CloseDialogCommand; use Drupal\Core\Ajax\MessageCommand; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Form\WorkspaceSafeFormInterface; use Drupal\Core\Url; use Drupal\media_library\MediaLibraryState; use Drupal\views\Attribute\ViewsField; @@ -22,7 +23,7 @@ use Symfony\Component\HttpFoundation\Request; * Plugin classes are internal. */ #[ViewsField("media_library_select_form")] -class MediaLibrarySelectForm extends FieldPluginBase { +class MediaLibrarySelectForm extends FieldPluginBase implements WorkspaceSafeFormInterface { /** * {@inheritdoc} diff --git a/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspacesMediaLibraryIntegrationTest.php b/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspacesMediaLibraryIntegrationTest.php new file mode 100644 index 00000000000..83ecb8e434f --- /dev/null +++ b/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspacesMediaLibraryIntegrationTest.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\workspaces\FunctionalJavascript; + +use Drupal\Tests\media_library\FunctionalJavascript\EntityReferenceWidgetTest; +use Drupal\user\UserInterface; +use Drupal\workspaces\Entity\Workspace; + +/** + * Tests the Media library entity reference widget in a workspace. + * + * @group workspaces + */ +class WorkspacesMediaLibraryIntegrationTest extends EntityReferenceWidgetTest { + + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'workspaces', + ]; + + /** + * An array of test methods that are not relevant for workspaces. + */ + const SKIP_METHODS = [ + // This test does not assert anything that can be workspace-specific. + 'testFocusNotAppliedWithoutSelectionChange', + // This test does not assert anything that can be workspace-specific. + 'testRequiredMediaField', + // This test tries to edit an entity in Live after it has been edited in a + // workspace, which is not currently possible. + 'testWidgetPreview', + ]; + + /** + * {@inheritdoc} + */ + public function setUp(): void { + if (in_array($this->name(), static::SKIP_METHODS, TRUE)) { + $this->markTestSkipped('Irrelevant for this test'); + } + + parent::setUp(); + + // Ensure that all the test methods are executed in the context of a + // workspace. + $stage = Workspace::load('stage'); + \Drupal::service('workspaces.manager')->setActiveWorkspace($stage); + } + + /** + * {@inheritdoc} + */ + protected function drupalCreateUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []): UserInterface|false { + // Ensure that users and roles are managed outside a workspace context. + return \Drupal::service('workspaces.manager')->executeOutsideWorkspace(function () use ($permissions, $name, $admin, $values) { + $permissions = array_merge($permissions, [ + 'view any workspace', + ]); + return parent::drupalCreateUser($permissions, $name, $admin, $values); + }); + } + +} |