diff --git a/core/modules/media/media.module b/core/modules/media/media.module index 706b5cb..28d3094 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -12,6 +12,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; +use Drupal\Component\Plugin\Factory\DefaultFactory; /** * Implements hook_help(). @@ -114,3 +115,67 @@ function template_preprocess_media(array &$variables) { $variables['content'][$key] = $variables['elements'][$key]; } } + +/** + * Implements hook_ENTITY_TYPE_presave(). + */ +function media_field_config_presave(FieldConfigInterface $field) { + // Don't change anything during a configuration sync. + if ($field->isSyncing()) { + return; + } + + // If this field formatter has not been configured yet, set the defaults to + // something more appropriate than the entity label. In our case, the + // source field. + if ($field->getSetting('handler') !== 'default:media' || empty($field->getSetting('handler_settings')['target_bundles'])) { + return; + } + + // If there are multiple target bundles allowed, we can't assume which field + // to use. + if (count($field->getSetting('handler_settings')['target_bundles']) > 1) { + return; + } + + $field_name = $field->get('field_name'); + $media_bundle = array_keys($field->getSetting('handler_settings')['target_bundles'])[0]; + $display = entity_get_display($field->get('entity_type'), $field->get('bundle'), 'default'); + + // At this point the display is already created and the formatter defaults + // were populated for this field type. It's tricky to reliably detect if the + // field is really new or just being edited. For now, we assume that if the + // field is not being used in any content, we can go ahead and set its display + // settings to the media source's defaults. + $num_entities = \Drupal::entityQuery($field->getTargetEntityTypeId()) + ->condition('type', $field->getTargetBundle()) + ->condition($field_name, NULL, 'IS NOT NULL') + ->count() + ->accessCheck(FALSE) + ->execute(); + if ($num_entities > 0) { + return; + } + + /** @var \Drupal\media\MediaTypeInterface $media_type */ + $media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($media_bundle); + $source_field_name = $media_type->getSource()->getConfiguration()['source_field']; + + // Use the default formatter + settings for the source field's type. + $source_field_type = $media_type->getSource()->getSourceFieldDefinition($media_type)->getType(); + if (empty($source_field_name) || empty($source_field_type)) { + return; + } + $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($source_field_type); + $source_field_formatter_type = $field_type_info['default_formatter']; + $source_field_formatter_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($source_field_formatter_type); + $display->setComponent($field_name, [ + 'type' => 'entity_reference_field', + 'settings' => [ + 'field_name' => $source_field_name, + 'type' => $source_field_formatter_type, + 'settings' => $source_field_formatter_settings, + 'label' => 'hidden', + ], + ])->save(); +}