function AddFormBase::createMediaFromValue

Same name in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::createMediaFromValue()
  2. 8.9.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::createMediaFromValue()
  3. 11.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::createMediaFromValue()

Creates a new, unsaved media item from a source field value.

Parameters

\Drupal\media\MediaTypeInterface $media_type: The media type of the media item.

\Drupal\Core\Entity\EntityStorageInterface $media_storage: The media storage.

string $source_field_name: The name of the media type's source field.

mixed $source_field_value: The value for the source field of the media item.

Return value

\Drupal\media\MediaInterface An unsaved media entity.

2 calls to AddFormBase::createMediaFromValue()
AddFormBase::processInputValues in core/modules/media_library/src/Form/AddFormBase.php
Creates media items from source field input values.
FileUploadForm::createMediaFromValue in core/modules/media_library/src/Form/FileUploadForm.php
Creates a new, unsaved media item from a source field value.
1 method overrides AddFormBase::createMediaFromValue()
FileUploadForm::createMediaFromValue in core/modules/media_library/src/Form/FileUploadForm.php
Creates a new, unsaved media item from a source field value.

File

core/modules/media_library/src/Form/AddFormBase.php, line 533

Class

AddFormBase
Provides a base class for creating media items from within the media library.

Namespace

Drupal\media_library\Form

Code

protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage, $source_field_name, $source_field_value) {
    $media = $media_storage->create([
        'bundle' => $media_type->id(),
        $source_field_name => $source_field_value,
    ]);
    $media->setName($media->getName());
    return $media;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.