function TermDevelGenerate::generateElements

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

Overrides DevelGenerateBase::generateElements

File

devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php, line 157

Class

TermDevelGenerate
Provides a TermDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateElements(array $values) : void {
    $new_terms = $this->generateTerms($values);
    if (!empty($new_terms['terms'])) {
        $this->setMessage($this->formatPlural($new_terms['terms'], 'Created 1 new term', 'Created @count new terms'));
        // Helper function to format the number of terms and the list of terms.
        $format_terms_func = function (array $data, $level) {
            if ($data['total'] > 10) {
                $data['terms'][] = '...';
            }
            return $this->formatPlural($data['total'], '1 new term at level @level (@terms)', '@count new terms at level @level (@terms)', [
                '@level' => $level,
                '@terms' => implode(',', $data['terms']),
            ]);
        };
        foreach ($new_terms['vocabs'] as $vid => $vlabel) {
            if (array_key_exists($vid, $new_terms)) {
                ksort($new_terms[$vid]);
                $termlist = implode(', ', array_map($format_terms_func, $new_terms[$vid], array_keys($new_terms[$vid])));
                $this->setMessage($this->t('In vocabulary @vlabel: @termlist', [
                    '@vlabel' => $vlabel,
                    '@termlist' => $termlist,
                ]));
            }
            else {
                $this->setMessage($this->t('In vocabulary @vlabel: No terms created', [
                    '@vlabel' => $vlabel,
                ]));
            }
        }
    }
    if ($new_terms['terms_translations'] > 0) {
        $this->setMessage($this->formatPlural($new_terms['terms_translations'], 'Created 1 term translation', 'Created @count term translations'));
    }
}