function MediaDevelGenerate::createMediaItem

Same name in other branches
  1. 5.x devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\MediaDevelGenerate::createMediaItem()

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

Parameters

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Thrown if the storage handler couldn't be loaded.

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown if the entity type doesn't exist.

\Drupal\Core\Entity\EntityStorageException Thrown if the bundle does not exist or was needed but not specified.

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

File

devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php, line 491

Class

MediaDevelGenerate
Provides a plugin that generates media entities.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function createMediaItem(array &$results) {
    if (!isset($results['time_range'])) {
        $results['time_range'] = 0;
    }
    $media_type = array_rand($results['media_types']);
    $uid = $results['users'][array_rand($results['users'])];
    $media = $this->mediaStorage
        ->create([
        'bundle' => $media_type,
        'name' => $this->getRandom()
            ->sentences(mt_rand(1, $results['name_length']), TRUE),
        'uid' => $uid,
        'revision' => mt_rand(0, 1),
        'status' => TRUE,
        'moderation_state' => 'published',
        'created' => $this->time
            ->getRequestTime() - 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();
}