You are here

public function MediaHelper::createFromInput in Varbase Media 9.0.x

Same name and namespace in other branches
  1. 8.7 modules/entity_browser_generic_embed/src/MediaHelper.php \Drupal\entity_browser_generic_embed\MediaHelper::createFromInput()
  2. 8.5 modules/entity_browser_generic_embed/src/MediaHelper.php \Drupal\entity_browser_generic_embed\MediaHelper::createFromInput()
  3. 8.6 modules/entity_browser_generic_embed/src/MediaHelper.php \Drupal\entity_browser_generic_embed\MediaHelper::createFromInput()

Creates a media entity from an input value.

Parameters

mixed $value: The input value.

string[] $bundles: (optional) A set of media bundle IDs which might match the input value. If omitted, all bundles to which the user has create access are checked.

Return value

\Drupal\media\MediaInterface The unsaved media entity.

File

modules/entity_browser_generic_embed/src/MediaHelper.php, line 122

Class

MediaHelper
Provides helper methods for dealing with media entities.

Namespace

Drupal\entity_browser_generic_embed

Code

public function createFromInput($value, array $bundles = []) {

  /** @var \Drupal\media\MediaInterface $entity */
  $entity = $this->entityTypeManager
    ->getStorage('media')
    ->create([
    'bundle' => $this
      ->getBundleFromInput($value, TRUE, $bundles)
      ->id(),
  ]);
  $field = static::getSourceField($entity);
  if ($field) {
    $field
      ->setValue($value);
  }
  return $entity;
}