5 * Contains \Drupal\Console\Utils\Create\NodeData.
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 NodeData extends Base
23 protected $bundles = [];
28 * @param EntityTypeManagerInterface $entityTypeManager
29 * @param EntityFieldManagerInterface $entityFieldManager
30 * @param DateFormatterInterface $dateFormatter
31 * @param array $bundles
33 public function __construct(
34 EntityTypeManagerInterface $entityTypeManager,
35 EntityFieldManagerInterface $entityFieldManager,
36 DateFormatterInterface $dateFormatter,
39 $this->bundles = $bundles;
48 * @param $contentTypes
55 public function create(
60 $language = LanguageInterface::LANGCODE_NOT_SPECIFIED
63 for ($i=0; $i<$limit; $i++) {
64 $contentType = $contentTypes[array_rand($contentTypes)];
65 $node = $this->entityTypeManager->getStorage('node')->create(
68 'type' => $contentType,
70 'created' => REQUEST_TIME - mt_rand(0, $timeRange),
71 'uid' => $this->getUserID(),
72 'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
73 'revision' => mt_rand(0, 1),
75 'promote' => mt_rand(0, 1),
76 'langcode' => $language
80 $this->generateFieldSampleData($node);
84 $nodes['success'][] = [
86 'node_type' => $this->bundles[$contentType],
87 'title' => $node->getTitle(),
88 'created' => $this->dateFormatter->format(
89 $node->getCreatedTime(),
94 } catch (\Exception $error) {
96 'node_type' => $this->bundles[$contentType],
97 'title' => $node->getTitle(),
98 'error' => $error->getMessage()