Commit 27ff71aa authored by xjm's avatar xjm

Issue #2934162 by marcoscano, phenaproxima, chr.fritsch, robpowell, Berdir,...

Issue #2934162 by marcoscano, phenaproxima, chr.fritsch, robpowell, Berdir, xjm, effulgentsia: Provide better defaults to media form displays
parent 60a2dc0b
......@@ -344,7 +344,12 @@ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayIn
* {@inheritdoc}
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName());
// Make sure the source field is placed just after the "name" basefield.
$name_component = $display->getComponent('name');
$source_field_weight = ($name_component && isset($name_component['weight'])) ? $name_component['weight'] + 5 : -50;
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'weight' => $source_field_weight,
]);
}
}
......@@ -22,18 +22,22 @@ class TestDifferentDisplays extends Test {
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'entity_reference_entity_id',
]);
parent::prepareViewDisplay($type, $display);
$source_name = $this->getSourceFieldDefinition($type)->getName();
$source_component = $display->getComponent($source_name) ?: [];
$source_component['type'] = 'entity_reference_entity_id';
$display->setComponent($source_name, $source_component);
}
/**
* {@inheritdoc}
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'entity_reference_autocomplete_tags',
]);
parent::prepareFormDisplay($type, $display);
$source_name = $this->getSourceFieldDefinition($type)->getName();
$source_component = $display->getComponent($source_name) ?: [];
$source_component['type'] = 'entity_reference_autocomplete_tags';
$display->setComponent($source_name, $source_component);
}
/**
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\Component\Utility\Html;
/**
* Tests the media type creation.
*
......@@ -54,6 +56,27 @@ public function testMediaTypeCreationFormWithDefaultField() {
$assert_session->fieldDisabled('Media source');
$assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
// Open up the media add form and verify that the source field is right
// after the name, and before the vertical tabs.
$this->drupalGet("/media/add/$mediaTypeMachineName");
// Get the form element, and its HTML representation.
$form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
$form = $assert_session->elementExists('css', $form_selector);
$form_html = $form->getOuterHtml();
// The name field should come before the source field, which should itself
// come before the vertical tabs.
$name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
$test_source_field = $assert_session->fieldExists('Test source', $form)->getOuterHtml();
$vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)->getOuterHtml();
$date_field = $assert_session->fieldExists('Date', $form)->getOuterHtml();
$published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
$this->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
$this->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));
// The "Published" checkbox should be the last element.
$this->assertTrue(strpos($form_html, $published_checkbox) > strpos($form_html, $date_field));
// Check that a new type with the same machine name cannot be created.
$this->drupalGet('admin/structure/media/add');
$page->fillField('label', $label);
......
......@@ -19,7 +19,7 @@ content:
settings: { }
third_party_settings: { }
field_media_audio_file:
weight: 1
weight: 0
settings:
progress_indicator: throbber
third_party_settings: { }
......@@ -27,7 +27,7 @@ content:
region: content
name:
type: string_textfield
weight: 0
weight: -5
region: content
settings:
size: 60
......
......@@ -22,7 +22,7 @@ content:
progress_indicator: throbber
third_party_settings: { }
type: file_generic
weight: 26
weight: 0
region: content
name:
type: string_textfield
......
......@@ -24,7 +24,7 @@ content:
preview_image_style: thumbnail
third_party_settings: { }
type: image_image
weight: 26
weight: 0
region: content
name:
type: string_textfield
......
......@@ -19,7 +19,7 @@ content:
settings: { }
third_party_settings: { }
field_media_video_file:
weight: 1
weight: 0
settings:
progress_indicator: throbber
third_party_settings: { }
......@@ -27,7 +27,7 @@ content:
region: content
name:
type: string_textfield
weight: 0
weight: -5
region: content
settings:
size: 60
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment