You are here

protected function TermDevelGenerate::generateTerms in Devel 8.3

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

array $parameters: The input parameters from the settings form.

Return value

array Information about 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 210

Class

TermDevelGenerate
Provides a TermDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateTerms(array $parameters) {
  $info = [
    'terms' => [],
    'terms_translations' => 0,
  ];
  $vocabs = $this->vocabularyStorage
    ->loadMultiple($parameters['vids']);

  // Insert new data:
  $max = $this->database
    ->query('SELECT MAX(tid) FROM {taxonomy_term_data}')
    ->fetchField();
  for ($i = 1; $i <= $parameters['num']; $i++) {
    $name = $this
      ->getRandom()
      ->word(mt_rand(2, $parameters['title_length']));
    $values = [
      'name' => $name,
      'description' => 'description of ' . $name,
      'format' => filter_fallback_format(),
      'weight' => mt_rand(0, 10),
    ];
    if (isset($parameters['add_language'])) {
      $values['langcode'] = $this
        ->getLangcode($parameters['add_language']);
    }
    switch ($i % 2) {
      case 1:
        $vocab = $vocabs[array_rand($vocabs)];
        $values['vid'] = $vocab
          ->id();
        $values['parent'] = [
          0,
        ];
        break;
      default:
        while (TRUE) {

          // Keep trying to find a random parent.
          $candidate = mt_rand(1, $max);
          $query = $this->database
            ->select('taxonomy_term_data', 't');
          $parent = $query
            ->fields('t', [
            'tid',
            'vid',
          ])
            ->condition('t.vid', array_keys($vocabs), 'IN')
            ->condition('t.tid', $candidate, '>=')
            ->range(0, 1)
            ->execute()
            ->fetchAssoc();
          if ($parent['tid']) {
            break;
          }
        }
        $values['parent'] = [
          $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();

    // Add translations.
    if (isset($parameters['translate_language']) && !empty($parameters['translate_language'])) {
      $info['terms_translations'] += $this
        ->generateTermTranslation($parameters['translate_language'], $term);
    }
    $max++;

    // Limit memory usage. Only report first 20 created terms.
    if ($i < 20) {
      $info['terms'][] = $term
        ->label();
    }
    unset($term);
  }
  return $info;
}