You are here

public function TermDevelGenerate::generateElements in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateElements()
  2. 8 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateElements()
  3. 8.2 devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateElements()

Business logic relating with each DevelGenerate plugin.

Parameters

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

Overrides DevelGenerateBase::generateElements

File

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

Class

TermDevelGenerate
Provides a TermDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

public function generateElements(array $values) {
  $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 ($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'));
  }
}