*/
class NodeData extends Base
{
- /* @var array */
- protected $bundles = [];
-
- /**
- * Nodes constructor.
- *
- * @param EntityTypeManagerInterface $entityTypeManager
- * @param EntityFieldManagerInterface $entityFieldManager
- * @param DateFormatterInterface $dateFormatter
- * @param array $bundles
- */
- public function __construct(
- EntityTypeManagerInterface $entityTypeManager,
- EntityFieldManagerInterface $entityFieldManager,
- DateFormatterInterface $dateFormatter,
- $bundles
- ) {
- $this->bundles = $bundles;
- parent::__construct(
- $entityTypeManager,
- $entityFieldManager,
- $dateFormatter
- );
- }
-
/**
* @param $contentTypes
* @param $limit
$language = LanguageInterface::LANGCODE_NOT_SPECIFIED
) {
$nodes = [];
- for ($i=0; $i<$limit; $i++) {
- $contentType = $contentTypes[array_rand($contentTypes)];
- $node = $this->entityTypeManager->getStorage('node')->create(
- [
- 'nid' => null,
- 'type' => $contentType,
-
- 'created' => REQUEST_TIME - mt_rand(0, $timeRange),
- 'uid' => $this->getUserID(),
- 'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
- 'revision' => mt_rand(0, 1),
- 'status' => true,
- 'promote' => mt_rand(0, 1),
- 'langcode' => $language
- ]
- );
-
- $this->generateFieldSampleData($node);
-
+ $bundles = $this->drupalApi->getBundles();
+ for ($i = 0; $i < $limit; $i++) {
try {
+ $contentType = $contentTypes[array_rand($contentTypes)];
+ $node = $this->entityTypeManager->getStorage('node')->create(
+ [
+ 'nid' => null,
+ 'type' => $contentType,
+ 'created' => REQUEST_TIME - mt_rand(0, $timeRange),
+ 'uid' => $this->getUserID(),
+ 'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
+ 'revision' => mt_rand(0, 1),
+ 'status' => true,
+ 'promote' => mt_rand(0, 1),
+ 'langcode' => $language
+ ]
+ );
+
+ $this->generateFieldSampleData($node);
$node->save();
$nodes['success'][] = [
'nid' => $node->id(),
- 'node_type' => $this->bundles[$contentType],
+ 'node_type' => $bundles[$contentType],
'title' => $node->getTitle(),
'created' => $this->dateFormatter->format(
$node->getCreatedTime(),
)
];
} catch (\Exception $error) {
- $nodes['error'][] = [
- 'node_type' => $this->bundles[$contentType],
- 'title' => $node->getTitle(),
- 'error' => $error->getMessage()
- ];
+ $nodes['error'][] = $error->getMessage();
}
}