Skip to content

Commit 63aedc4

Browse files
committed
Issue #3018539 by phenaproxima, rodrigoaguilera, alexpott: Media types cannot be created in the UI without JavaScript
(cherry picked from commit 3ba6bc1)
1 parent b7f2615 commit 63aedc4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

modules/media/src/MediaTypeForm.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ function ($item) {
304304
*/
305305
protected function actions(array $form, FormStateInterface $form_state) {
306306
$actions = parent::actions($form, $form_state);
307+
308+
// If the media source has not been chosen yet, turn the submit button into
309+
// a button. This rebuilds the form with the media source's configuration
310+
// form visible, instead of saving the media type. This allows users to
311+
// create a media type without JavaScript enabled. With JavaScript enabled,
312+
// this rebuild occurs during an AJAX request.
313+
// @see \Drupal\media\MediaTypeForm::ajaxHandlerData()
314+
if (empty($this->getEntity()->get('source'))) {
315+
$actions['submit']['#type'] = 'button';
316+
}
317+
307318
$actions['submit']['#value'] = $this->t('Save');
308319
$actions['delete']['#value'] = $this->t('Delete');
309320
$actions['delete']['#access'] = $this->entity->access('delete');
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Drupal\Tests\media\Functional;
4+
5+
use Drupal\media\Entity\MediaType;
6+
7+
/**
8+
* Ensures that media UI works correctly without JavaScript.
9+
*
10+
* @group media
11+
*/
12+
class MediaTypeCreationTest extends MediaFunctionalTestBase {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public static $modules = [
18+
'media_test_source',
19+
];
20+
21+
/**
22+
* Tests the media type creation form with only the mandatory options.
23+
*/
24+
public function testMediaTypeCreationForm() {
25+
$machine_name = mb_strtolower($this->randomMachineName());
26+
27+
$this->drupalGet('/admin/structure/media/add');
28+
$this->assertSession()->statusCodeEquals(200);
29+
$this->assertSession()->fieldExists('label')->setValue($this->randomString());
30+
$this->assertSession()->fieldExists('id')->setValue($machine_name);
31+
$this->assertSession()->selectExists('source')->selectOption('test');
32+
$this->assertSession()->buttonExists('Save')->press();
33+
$this->assertSession()->statusCodeEquals(200);
34+
$this->assertSession()->fieldValueEquals('Test config value', 'This is default value.');
35+
$this->assertSession()->buttonExists('Save')->press();
36+
$this->assertSession()->statusCodeEquals(200);
37+
$this->assertSession()->addressEquals('admin/structure/media');
38+
39+
$this->assertInstanceOf(MediaType::class, MediaType::load($machine_name));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)