protected function TermDevelGenerate::generateTerms in Devel 8.2
Same name and namespace in other branches
- 8.3 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateTerms()
- 8 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateTerms()
- 4.x devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateTerms()
Generates taxonomy terms for a list of given vocabularies.
Parameters
int $records: Number of terms to create in total.
\Drupal\taxonomy\TermInterface[] $vocabs: List of vocabularies to populate.
int $maxlength: (optional) Maximum length per term.
Return value
array The list of names of the created terms.
1 call to TermDevelGenerate::generateTerms()
- TermDevelGenerate::generateElements in devel_generate/
src/ Plugin/ DevelGenerate/ TermDevelGenerate.php - Business logic relating with each DevelGenerate plugin
File
- devel_generate/
src/ Plugin/ DevelGenerate/ TermDevelGenerate.php, line 159
Class
- TermDevelGenerate
- Provides a TermDevelGenerate plugin.
Namespace
Drupal\devel_generate\Plugin\DevelGenerateCode
protected function generateTerms($records, $vocabs, $maxlength = 12) {
$terms = array();
// Insert new data:
$max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')
->fetchField();
$start = time();
for ($i = 1; $i <= $records; $i++) {
$name = $this
->getRandom()
->word(mt_rand(2, $maxlength));
$values = array(
'name' => $name,
'description' => 'description of ' . $name,
'format' => filter_fallback_format(),
'weight' => mt_rand(0, 10),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
);
switch ($i % 2) {
case 1:
$vocab = $vocabs[array_rand($vocabs)];
$values['vid'] = $vocab
->id();
$values['parent'] = array(
0,
);
break;
default:
while (TRUE) {
// Keep trying to find a random parent.
$candidate = mt_rand(1, $max);
$query = db_select('taxonomy_term_data', 't');
$parent = $query
->fields('t', array(
'tid',
'vid',
))
->condition('t.vid', array_keys($vocabs), 'IN')
->condition('t.tid', $candidate, '>=')
->range(0, 1)
->execute()
->fetchAssoc();
if ($parent['tid']) {
break;
}
}
$values['parent'] = array(
$parent['tid'],
);
// Slight speedup due to this property being set.
$values['vid'] = $parent['vid'];
break;
}
$term = $this->termStorage
->create($values);
// A flag to let hook implementations know that this is a generated term.
$term->devel_generate = TRUE;
// Populate all fields with sample values.
$this
->populateFields($term);
$term
->save();
$max++;
// Limit memory usage. Only report first 20 created terms.
if ($i < 20) {
$terms[] = $term
->label();
}
unset($term);
}
return $terms;
}