You are here

protected function VocabularyDevelGenerate::generateVocabularies in Devel 8.2

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate::generateVocabularies()
  2. 8 devel_generate/src/Plugin/DevelGenerate/VocabularyDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\VocabularyDevelGenerate::generateVocabularies()
  3. 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 128

Class

VocabularyDevelGenerate
Provides a VocabularyDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateVocabularies($records, $maxlength = 12) {
  $vocabularies = array();

  // Insert new data:
  for ($i = 1; $i <= $records; $i++) {
    $name = $this
      ->getRandom()
      ->word(mt_rand(2, $maxlength));
    $vocabulary = $this->vocabularyStorage
      ->create(array(
      'name' => $name,
      'vid' => mb_strtolower($name),
      'langcode' => Language::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;
}