diff --git a/core/modules/media/src/Ajax/OpenUrlCommand.php b/core/modules/media/src/Ajax/MediaOpenUrlCommand.php similarity index 75% rename from core/modules/media/src/Ajax/OpenUrlCommand.php rename to core/modules/media/src/Ajax/MediaOpenUrlCommand.php index 4da382a5e7..348cd076c7 100644 --- a/core/modules/media/src/Ajax/OpenUrlCommand.php +++ b/core/modules/media/src/Ajax/MediaOpenUrlCommand.php @@ -8,7 +8,7 @@ /** * AJAX command to open a URL in a modal dialog. */ -class OpenUrlCommand implements CommandInterface { +class MediaOpenUrlCommand implements CommandInterface { /** * The URL to open. @@ -18,13 +18,13 @@ class OpenUrlCommand implements CommandInterface { protected $url; /** - * Constructs a OpenUrlCommand object. + * Constructs a MediaOpenUrlCommand object. * * @param \Drupal\Core\Url|string $url * The URL to open. */ public function __construct($url) { - $this->url = $url instanceof Url ? $url->toString() : $url; + $this->url = $url instanceof Url ? $url->toString(TRUE) : $url; } /** diff --git a/core/modules/media/src/Controller/MediaController.php b/core/modules/media/src/Controller/MediaController.php index b798bc80f8..04e8f5f24a 100644 --- a/core/modules/media/src/Controller/MediaController.php +++ b/core/modules/media/src/Controller/MediaController.php @@ -4,10 +4,15 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Entity\Controller\EntityController; +use Drupal\Core\Entity\EntityRepositoryInterface; +use Drupal\Core\Entity\EntityTypeBundleInfoInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; use Drupal\file\FileInterface; -use Drupal\media\Ajax\OpenUrlCommand; -use Drupal\media\MediaSourceInterface; +use Drupal\media\Ajax\MediaOpenUrlCommand; use Drupal\media\MediaTypeInterface; use Symfony\Component\HttpFoundation\Request; @@ -24,6 +29,26 @@ class MediaController extends EntityController { protected $request; /** + * The first available file in the URL. + * + * @var \Drupal\file\FileInterface + */ + protected $file; + + /** + * {@inheritdoc} + */ + public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, Request $request = NULL) { + parent::__construct($entity_type_manager, $entity_type_bundle_info, $entity_repository, $renderer, $string_translation, $url_generator); + $fids = $request->get('fids'); + if ($fids) { + $this->file = $this->entityTypeManager + ->getStorage('file') + ->load($fids[0]); + } + } + + /** * {@inheritdoc} */ public function addPage($entity_type_id, Request $request = NULL) { @@ -45,15 +70,9 @@ public function addPage($entity_type_id, Request $request = NULL) { $types = array_intersect_key($types, array_flip($handler_settings['target_bundles'])); } - $fids = $this->request->get('fids'); - if ($fids) { - /** @var \Drupal\file\FileInterface $file */ - $file = $this->entityTypeManager - ->getStorage('file') - ->load($fids[0]); - + if ($this->file) { // Only allow types whose source field supports the file. - $types = array_intersect_key($types, $this->getSupportedTypesForFile($file, array_keys($types))); + $types = array_intersect_key($types, $this->getSupportedTypesForFile($this->file, array_keys($types))); } // Redirect to media add form if we only found 1 type. @@ -78,14 +97,9 @@ public function addTitle($entity_type_id, Request $request = NULL) { $this->request = $request; $title = parent::addTitle($entity_type_id); - // Change title for the file widget modal. - if ($this->request->get('modal') == 'media_file') { - $fids = $request->get('fids'); - /** @var \Drupal\file\FileInterface $file */ - $file = $this->entityTypeManager - ->getStorage('file') - ->load($fids[0]); - $title = $this->t('Add @filename', ['@filename' => $file->getFilename()]); + // Change title when a file has already been uploaded. + if ($this->request->get('modal') === 'media_file' && $this->file) { + $title = $this->t('Add @filename', ['@filename' => $this->file->getFilename()]); } return $title; @@ -128,7 +142,7 @@ protected function redirect($route_name, array $route_parameters = [], array $op // Change redirect response for the file widget modal. if ($this->request->get('modal') == 'media_file') { return (new AjaxResponse) - ->addCommand(new OpenUrlCommand($redirect->getTargetUrl())); + ->addCommand(new MediaOpenUrlCommand($redirect->getTargetUrl())); } else { return $redirect; @@ -156,11 +170,9 @@ protected function getSupportedTypesForFile(FileInterface $file, array $types = return array_filter($types, function (MediaTypeInterface $type) use ($extension) { $handler = $type->getSource(); - if ($handler instanceof MediaSourceInterface) { - $field = $handler->getSourceFieldDefinition($type); - if (in_array($field->getType(), ['file', 'image'])) { - return in_array($extension, explode(' ', $field->getSetting('file_extensions'))); - } + $field = $handler->getSourceFieldDefinition($type); + if (in_array($field->getType(), ['file'])) { + return in_array($extension, explode(' ', $field->getSetting('file_extensions'))); } return FALSE; }); diff --git a/core/modules/media/src/MediaForm.php b/core/modules/media/src/MediaForm.php index 050f1f7eec..49712f463e 100644 --- a/core/modules/media/src/MediaForm.php +++ b/core/modules/media/src/MediaForm.php @@ -8,7 +8,7 @@ use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; -use Drupal\media\Ajax\OpenUrlCommand; +use Drupal\media\Ajax\MediaOpenUrlCommand; /** * Form controller for the media edit forms. @@ -263,7 +263,7 @@ public function fileWidgetAjaxSubmit(array &$form, FormStateInterface $form_stat 'fids' => $fids, ], ]); - $response->addCommand(new OpenUrlCommand($url)); + $response->addCommand(new MediaOpenUrlCommand($url)); } else { $response->addCommand(new CloseModalDialogCommand()); diff --git a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php index 8e656eb857..b0064553ed 100644 --- a/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php +++ b/core/modules/media/src/Plugin/Field/FieldWidget/MediaFileWidget.php @@ -19,7 +19,7 @@ use Drupal\file\Element\ManagedFile; use Drupal\file\Entity\File; use Drupal\file\Plugin\Field\FieldType\FileItem; -use Drupal\media\Ajax\OpenUrlCommand; +use Drupal\media\Ajax\MediaOpenUrlCommand; use Drupal\media\Entity\MediaType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -320,6 +320,7 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f '#cardinality' => $cardinality, ]; $elements['upload'][$delta][$field_name . '_media_file_upload'] = [ + '#title' => $title, '#type' => 'managed_file', '#required' => $delta == 0 && $this->fieldDefinition->isRequired(), '#upload_validators' => $upload_validators, @@ -360,6 +361,8 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f $elements['#title'] = $title; $elements['#description'] = $description; $elements['#field_name'] = $field_name; + unset($elements['upload'][$delta][$field_name . '_media_file_upload']['#title']); + unset($elements['upload'][$delta][$field_name . '_media_file_upload']['#description']); } return $elements; @@ -646,7 +649,7 @@ public static function uploadFieldAjaxCallback(array &$form, FormStateInterface 'fids' => $element['#value']['fids'], ], ]); - $response->addCommand(new OpenUrlCommand($url)); + $response->addCommand(new MediaOpenUrlCommand($url)); } else {