function ContentDevelGenerate::develGenerateContentAddNode
Same name in other branches
- 4.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::develGenerateContentAddNode()
Create one node. Used by both batch and non-batch code branches.
Parameters
array $results: Results information.
2 calls to ContentDevelGenerate::develGenerateContentAddNode()
- ContentDevelGenerate::batchContentAddNode in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php - Batch wrapper for calling ContentAddNode.
- ContentDevelGenerate::generateContent in devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php - Generate content when not in batch mode.
File
-
devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php, line 649
Class
- ContentDevelGenerate
- Provides a ContentDevelGenerate plugin.
Namespace
Drupal\devel_generate\Plugin\DevelGenerateCode
protected function develGenerateContentAddNode(array &$results) : void {
if (!isset($results['time_range'])) {
$results['time_range'] = 0;
}
$users = $results['users'];
$node_type = array_rand($results['node_types']);
$uid = $users[array_rand($users)];
// Add the content type label if required.
$title_prefix = $results['add_type_label'] ? $this->nodeTypeStorage
->load($node_type)
->label() . ' - ' : '';
$values = [
'nid' => NULL,
'type' => $node_type,
'title' => $title_prefix . $this->getRandom()
->sentences(mt_rand(1, $results['title_length']), TRUE),
'uid' => $uid,
'revision' => mt_rand(0, 1),
'moderation_state' => 'published',
'status' => TRUE,
'promote' => mt_rand(0, 1),
'created' => $this->time
->getRequestTime() - mt_rand(0, $results['time_range']),
// A flag to let hook_node_insert() implementations know that this is a
// generated node.
'devel_generate' => $results,
];
if (isset($results['add_language'])) {
$values['langcode'] = $this->getLangcode($results['add_language']);
}
/** @var \Drupal\node\NodeInterface $node */
$node = $this->nodeStorage
->create($values);
// Populate non-skipped fields with sample values.
$this->populateFields($node, $results['skip_fields'], $results['base_fields']);
// Remove the fields which are intended to have no value.
foreach ($results['skip_fields'] as $field) {
unset($node->{$field});
}
$node->save();
$this->insertNodeData($node);
// Add url alias if required.
if (!empty($results['add_alias'])) {
$path_alias = $this->aliasStorage
->create([
'path' => '/node/' . $node->id(),
'alias' => '/node-' . $node->id() . '-' . $node->bundle(),
'langcode' => $values['langcode'] ?? LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$path_alias->save();
}
// Add translations.
$this->develGenerateContentAddNodeTranslation($results, $node);
}