Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Utils / Create / NodeData.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Utils\Create\NodeData.
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 Nodes
17  *
18  * @package Drupal\Console\Utils
19  */
20 class NodeData extends Base
21 {
22     /**
23      * @param $contentTypes
24      * @param $limit
25      * @param $titleWords
26      * @param $timeRange
27      *
28      * @return array
29      */
30     public function create(
31         $contentTypes,
32         $limit,
33         $titleWords,
34         $timeRange,
35         $language = LanguageInterface::LANGCODE_NOT_SPECIFIED
36     ) {
37         $nodes = [];
38         $bundles = $this->drupalApi->getBundles();
39         for ($i = 0; $i < $limit; $i++) {
40             try {
41                 $contentType = $contentTypes[array_rand($contentTypes)];
42                 $node = $this->entityTypeManager->getStorage('node')->create(
43                     [
44                         'nid' => null,
45                         'type' => $contentType,
46                         'created' => REQUEST_TIME - mt_rand(0, $timeRange),
47                         'uid' => $this->getUserID(),
48                         'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
49                         'revision' => mt_rand(0, 1),
50                         'status' => true,
51                         'promote' => mt_rand(0, 1),
52                         'langcode' => $language
53                     ]
54                 );
55
56                 $this->generateFieldSampleData($node);
57                 $node->save();
58                 $nodes['success'][] = [
59                     'nid' => $node->id(),
60                     'node_type' => $bundles[$contentType],
61                     'title' => $node->getTitle(),
62                     'created' => $this->dateFormatter->format(
63                         $node->getCreatedTime(),
64                         'custom',
65                         'Y-m-d h:i:s'
66                     )
67                 ];
68             } catch (\Exception $error) {
69                 $nodes['error'][] = $error->getMessage();
70             }
71         }
72
73         return $nodes;
74     }
75 }