function VocabularyDevelGenerate::generateVocabularies

Same name in other branches
  1. 4.x devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate::generateVocabularies()

Generates vocabularies.

Parameters

int $records: Number of vocabularies to create.

int $maxlength: (optional) Maximum length for vocabulary name.

Return value

array Array containing the generated vocabularies id.

1 call to VocabularyDevelGenerate::generateVocabularies()
VocabularyDevelGenerate::generateElements in devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php
Business logic relating with each DevelGenerate plugin.

File

devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php, line 111

Class

VocabularyDevelGenerate
Provides a VocabularyDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateVocabularies(int $records, int $maxlength = 12) : array {
    $vocabularies = [];
    // Insert new data:
    for ($i = 1; $i <= $records; ++$i) {
        $name = $this->getRandom()
            ->word(mt_rand(2, $maxlength));
        $vocabulary = $this->vocabularyStorage
            ->create([
            'name' => $name,
            'vid' => mb_strtolower($name),
            'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
            'description' => 'Description of ' . $name,
            'hierarchy' => 1,
            'weight' => mt_rand(0, 10),
            'multiple' => 1,
            'required' => 0,
            'relations' => 1,
        ]);
        // Populate all fields with sample values.
        $this->populateFields($vocabulary);
        $vocabulary->save();
        $vocabularies[] = $vocabulary->id();
        unset($vocabulary);
    }
    return $vocabularies;
}