diff --git a/core/modules/media/media.module b/core/modules/media/media.module index 88b28f3..706b5cb 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -114,81 +114,3 @@ 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'); - - // @TODO How to detect if we need to define the formatter? At this point the - // display is already created and the formatter defaults were populated for - // this field type. How to detect then if the defaults were automattically - // set, or if the user really chose those settings? For now we assume that if - // the field is not used in any content, it is safe to change the display - // settings, but it may be too much to assume. - $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']; - // Take decision on which formatter to use based on known source plugins. - // @TODO Maybe we should move this decision to the source plugins? This would - // allow contrib sources also to provide their preferred default formatters. - $source_field_formatter_type = FALSE; - $source_field_formatter_settings = []; - switch ($media_type->getSource()->getPluginId()) { - case 'image': - $source_field_formatter_type = 'image'; - $source_field_formatter_settings = [ - 'image_style' => '', - 'image_link' => '', - ]; - break; - - case 'file': - $source_field_formatter_type = 'file_default'; - break; - - } - - if ($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(); - } -}