Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Utils / Create / TermData.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Create\TermData.
6  */
7
8 namespace Drupal\Console\Utils\Create;
9
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Entity\EntityFieldManagerInterface;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Language\LanguageInterface;
14
15 /**
16  * Class Terms
17  *
18  * @package Drupal\Console\Utils
19  */
20 class TermData extends Base
21 {
22     /**
23      * Create and returns an array of new Terms.
24      *
25      * @param $vocabularies
26      * @param $limit
27      * @param $nameWords
28      *
29      * @return array
30      */
31     public function create(
32         $vocabularies,
33         $limit,
34         $nameWords
35     ) {
36         $siteVocabularies = $this->drupalApi->getVocabularies();
37         $terms = [];
38         for ($i = 0; $i < $limit; $i++) {
39             try {
40                 $vocabulary = $vocabularies[array_rand($vocabularies)];
41                 $term = $this->entityTypeManager->getStorage('taxonomy_term')->create(
42                     [
43                         'vid' => $vocabulary,
44                         'name' => $this->getRandom()->sentences(mt_rand(1, $nameWords), true),
45                         'description' => [
46                             'value' => $this->getRandom()->sentences(mt_rand(1, $nameWords)),
47                             'format' => 'full_html',
48                         ],
49                         'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
50                     ]
51                 );
52                 $term->save();
53                 $terms['success'][] = [
54                     'tid' => $term->id(),
55                     'vocabulary' => $siteVocabularies[$vocabulary],
56                     'name' => $term->getName(),
57                 ];
58             } catch (\Exception $error) {
59                 $terms['error'][] = $error->getMessage();
60             }
61         }
62
63         return $terms;
64     }
65 }