You are here

public function MediaHelper::createFromInput in Lightning Media 8.4

Same name and namespace in other branches
  1. 8 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::createFromInput()
  2. 8.2 src/MediaHelper.php \Drupal\lightning_media\MediaHelper::createFromInput()
  3. 8.3 src/MediaHelper.php \Drupal\lightning_media\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

src/MediaHelper.php, line 142

Class

MediaHelper
Provides helper methods for dealing with media entities.

Namespace

Drupal\lightning_media

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;
}