You are here

protected function MediaDevelGenerate::createMediaItem in Media entity 8

Create one media item. Used by both batch and non-batch code branches.

Parameters

array $results: The input values from the settings form.

2 calls to MediaDevelGenerate::createMediaItem()
MediaDevelGenerate::batchCreateMediaItem in src/Plugin/DevelGenerate/MediaDevelGenerate.php
Batch version of createMediaItem().
MediaDevelGenerate::generateMedia in src/Plugin/DevelGenerate/MediaDevelGenerate.php
Method for creating media when number of elements is less than 50.

File

src/Plugin/DevelGenerate/MediaDevelGenerate.php, line 413

Class

MediaDevelGenerate
Provides a MediaDevelGenerate plugin.

Namespace

Drupal\media_entity\Plugin\DevelGenerate

Code

protected function createMediaItem(&$results) {
  if (!isset($results['time_range'])) {
    $results['time_range'] = 0;
  }
  $users = $results['users'];
  $bundle = array_rand(array_filter($results['media_bundles']));
  $uid = $users[array_rand($users)];
  $media = $this->mediaStorage
    ->create([
    'bundle' => $bundle,
    'name' => $this
      ->getRandom()
      ->sentences(mt_rand(1, $results['name_length']), TRUE),
    'uid' => $uid,
    'revision' => mt_rand(0, 1),
    'status' => TRUE,
    'created' => REQUEST_TIME - mt_rand(0, $results['time_range']),
    'langcode' => $this
      ->getLangcode($results),
  ]);

  // A flag to let hook implementations know that this is a generated item.
  $media->devel_generate = $results;

  // Populate all fields with sample values.
  $this
    ->populateFields($media);
  $media
    ->save();
}