3521bc728dea334d23abb8657d4a0048c0d100e5
[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     /* @var array */
23     protected $vocabularies = [];
24
25     /**
26      * Terms constructor.
27      *
28      * @param EntityTypeManagerInterface  $entityTypeManager
29      * @param EntityFieldManagerInterface $entityFieldManager
30      * @param DateFormatterInterface      $dateFormatter
31      * @param array                       $vocabularies
32      */
33     public function __construct(
34         EntityTypeManagerInterface $entityTypeManager,
35         EntityFieldManagerInterface $entityFieldManager,
36         DateFormatterInterface $dateFormatter,
37         $vocabularies
38     ) {
39         $this->vocabularies = $vocabularies;
40         parent::__construct(
41             $entityTypeManager,
42             $entityFieldManager,
43             $dateFormatter
44         );
45     }
46
47     /**
48      * Create and returns an array of new Terms.
49      *
50      * @param $vocabularies
51      * @param $limit
52      * @param $nameWords
53      *
54      * @return array
55      */
56     public function create(
57         $vocabularies,
58         $limit,
59         $nameWords
60     ) {
61         $terms = [];
62         for ($i=0; $i<$limit; $i++) {
63             $vocabulary = $vocabularies[array_rand($vocabularies)];
64             $term = $this->entityTypeManager->getStorage('taxonomy_term')->create(
65                 [
66                     'vid' => $vocabulary,
67                     'name' => $this->getRandom()->sentences(mt_rand(1, $nameWords), true),
68                     'description' => [
69                         'value' => $this->getRandom()->sentences(),
70                         'format' => 'full_html',
71                     ],
72                     'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
73                 ]
74             );
75
76             try {
77                 $term->save();
78                 $terms['success'][] = [
79                     'tid' => $term->id(),
80                     'vocabulary' => $this->vocabularies[$vocabulary],
81                     'name' => $term->getName(),
82                 ];
83             } catch (\Exception $error) {
84                 $terms['error'][] = [
85                     'vocabulary' => $this->vocabularies[$vocabulary],
86                     'name' => $term->getName(),
87                     'error' => $error->getMessage()
88                 ];
89             }
90         }
91
92         return $terms;
93     }
94 }