5 * Contains \Drupal\Console\Utils\Create\TermData.
8 namespace Drupal\Console\Utils\Create;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Entity\EntityFieldManagerInterface;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Language\LanguageInterface;
18 * @package Drupal\Console\Utils
20 class TermData extends Base
23 protected $vocabularies = [];
28 * @param EntityTypeManagerInterface $entityTypeManager
29 * @param EntityFieldManagerInterface $entityFieldManager
30 * @param DateFormatterInterface $dateFormatter
31 * @param array $vocabularies
33 public function __construct(
34 EntityTypeManagerInterface $entityTypeManager,
35 EntityFieldManagerInterface $entityFieldManager,
36 DateFormatterInterface $dateFormatter,
39 $this->vocabularies = $vocabularies;
48 * Create and returns an array of new Terms.
50 * @param $vocabularies
56 public function create(
62 for ($i=0; $i<$limit; $i++) {
63 $vocabulary = $vocabularies[array_rand($vocabularies)];
64 $term = $this->entityTypeManager->getStorage('taxonomy_term')->create(
67 'name' => $this->getRandom()->sentences(mt_rand(1, $nameWords), true),
69 'value' => $this->getRandom()->sentences(),
70 'format' => 'full_html',
72 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
78 $terms['success'][] = [
80 'vocabulary' => $this->vocabularies[$vocabulary],
81 'name' => $term->getName(),
83 } catch (\Exception $error) {
85 'vocabulary' => $this->vocabularies[$vocabulary],
86 'name' => $term->getName(),
87 'error' => $error->getMessage()