You are here

protected function FileUploadForm::getMediaType in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Form/FileUploadForm.php \Drupal\media_library\Form\FileUploadForm::getMediaType()

Get the media type from the form state.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

\Drupal\media\MediaTypeInterface The media type.

Throws

\InvalidArgumentException If the selected media type does not exist.

Overrides AddFormBase::getMediaType

1 call to FileUploadForm::getMediaType()
FileUploadForm::buildInputElement in core/modules/media_library/src/Form/FileUploadForm.php
Builds the element for submitting source field value(s).

File

core/modules/media_library/src/Form/FileUploadForm.php, line 113

Class

FileUploadForm
Creates a form to create media entities from uploaded files.

Namespace

Drupal\media_library\Form

Code

protected function getMediaType(FormStateInterface $form_state) {
  if ($this->mediaType) {
    return $this->mediaType;
  }
  $media_type = parent::getMediaType($form_state);

  // The file upload form only supports media types which use a file field as
  // a source field.
  $field_definition = $media_type
    ->getSource()
    ->getSourceFieldDefinition($media_type);
  if (!is_a($field_definition
    ->getClass(), FileFieldItemList::class, TRUE)) {
    throw new \InvalidArgumentException('Can only add media types which use a file field as a source field.');
  }
  return $media_type;
}