Issue #3081233 by phenaproxima, webchick, Wim Leers, oknate, xjm: The default display for new media types should only show the source field

merge-requests/55/head
webchick 2019-10-04 14:50:48 -07:00
parent 67dd0adb63
commit 9aa8a35cd2
10 changed files with 39 additions and 39 deletions

View File

@ -16,7 +16,6 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Drupal\media\Plugin\media\Source\Image;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Drupal\views\ViewExecutable;
@ -517,36 +516,6 @@ function media_field_widget_form_alter(&$element, FormStateInterface $form_state
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function media_form_media_type_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$form['actions']['submit']['#submit'][] = '_media_type_add_form_submit';
}
/**
* Submit callback for media_type_add_form.
*/
function _media_type_add_form_submit(array &$form, FormStateInterface $form_state) {
$type = $form_state->getFormObject()->getEntity();
if (!is_a($type->getSource(), Image::class, TRUE)) {
return;
}
// Load the default entity view display.
$display = \Drupal::service('entity_display.repository')
->getViewDisplay('media', $type->id());
// Remove all default components.
foreach (array_keys($display->getComponents()) as $name) {
$display->removeComponent($name);
}
$type->getSource()->prepareViewDisplay($type, $display);
$display->save();
}
/**
* Implements hook_views_query_substitutions().
*/

View File

@ -339,7 +339,9 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName());
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'label' => 'visually_hidden',
]);
}
/**

View File

@ -363,6 +363,11 @@ class MediaTypeForm extends EntityForm {
}
if ($source_field->isDisplayConfigurable('view')) {
$display = $this->entityDisplayRepository->getViewDisplay('media', $media_type->id());
// Remove all default components.
foreach (array_keys($display->getComponents()) as $name) {
$display->removeComponent($name);
}
$source->prepareViewDisplay($media_type, $display);
$display->save();
}

View File

@ -33,6 +33,7 @@ class AudioFile extends File {
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_audio',
'label' => 'visually_hidden',
]);
}

View File

@ -174,7 +174,6 @@ class Image extends File {
// set an image style.
$field_name = $this->getSourceFieldDefinition($type)->getName();
$component = $display->getComponent($field_name);
$component['label'] = 'visually_hidden';
$component['settings']['image_link'] = '';
$component['settings']['image_style'] = '';
if ($this->entityTypeManager->getStorage('image_style')->load('large')) {

View File

@ -442,6 +442,7 @@ class OEmbed extends MediaSourceBase implements OEmbedInterface {
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'oembed',
'label' => 'visually_hidden',
]);
}

View File

@ -33,6 +33,7 @@ class VideoFile extends File {
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_video',
'label' => 'visually_hidden',
]);
}

View File

@ -62,8 +62,10 @@ class MediaSourceFileTest extends MediaSourceTestBase {
// Get the media entity view URL from the creation message.
$this->drupalGet($this->assertLinkToCreatedMedia());
// Make sure the thumbnail is displayed.
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png');
// Make sure a link to the file is displayed.
$assert_session->linkExists($test_filename);
// The thumbnail should not be displayed.
$assert_session->elementNotExists('css', '.image-style-thumbnail');
// Make sure checkbox changes the visibility of log message field.
$this->drupalGet("media/1/edit");

View File

@ -141,8 +141,10 @@ class MediaSourceOEmbedVideoTest extends MediaSourceTestBase {
$this->assertSame('480', $session->evaluateScript("$inner_frame.getAttribute('width')"));
$this->assertLessThanOrEqual(240, $session->evaluateScript("$inner_frame.clientWidth"));
// Make sure the thumbnail is displayed from uploaded image.
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', '/oembed_thumbnails/' . basename($thumbnail));
// The oEmbed content iFrame should be visible.
$assert_session->elementExists('css', 'iframe.media-oembed-content');
// The thumbnail should not be displayed.
$assert_session->elementNotExists('css', '.image-style-thumbnail');
// Load the media and check that all fields are properly populated.
$media = Media::load(1);

View File

@ -113,11 +113,14 @@ abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
* The media source ID.
* @param array $provided_fields
* (optional) An array of field machine names this type provides.
* @param string $source_label_visibility
* (optional) The visibility that the source field label is expected to
* have. Defaults to 'visually_hidden'.
*
* @return \Drupal\media\MediaTypeInterface
* The created media type.
*/
public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = []) {
public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = [], $source_label_visibility = 'visually_hidden') {
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
@ -146,12 +149,27 @@ abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
$this->drupalGet('admin/structure/media');
$assert_session->pageTextContains($media_type_id);
$media_type = MediaType::load($media_type_id);
// Assert that the default display of the media type only shows the source
// field.
$this->drupalGet("/admin/structure/media/manage/$media_type_id/display");
// There should be only one field with editable settings, and it should be
// the source field.
$assert_session->elementsCount('css', 'input[name$="_settings_edit"]', 1);
$source_field_name = $media_type->getSource()
->getSourceFieldDefinition($media_type)
->getName();
$assert_session->buttonExists("{$source_field_name}_settings_edit");
// Ensure the source field label is configured as expected.
$assert_session->fieldValueEquals("fields[$source_field_name][label]", $source_label_visibility);
// Bundle definitions are statically cached in the context of the test, we
// need to make sure we have updated information before proceeding with the
// actions on the UI.
\Drupal::service('entity_type.bundle.info')->clearCachedBundles();
return MediaType::load($media_type_id);
return $media_type;
}
}