You are here

protected function ContentDevelGenerate::develGenerateContentAddNode in Devel 4.x

Same name and namespace in other branches
  1. 8.3 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::develGenerateContentAddNode()
  2. 8 devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::develGenerateContentAddNode()
  3. 8.2 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 621

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function develGenerateContentAddNode(array &$results) {
  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),
    'status' => TRUE,
    'promote' => mt_rand(0, 1),
    'created' => $this->time
      ->getRequestTime() - mt_rand(0, $results['time_range']),
  ];
  if (isset($results['add_language'])) {
    $values['langcode'] = $this
      ->getLangcode($results['add_language']);
  }
  $node = $this->nodeStorage
    ->create($values);

  // A flag to let hook_node_insert() implementations know that this is a
  // generated node.
  $node->devel_generate = $results;

  // Populate all fields with sample values.
  $this
    ->populateFields($node);

  // See devel_generate_entity_insert() for actions that happen before and
  // after this save.
  $node
    ->save();

  // 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.
  if (isset($results['translate_language']) && !empty($results['translate_language'])) {
    $this
      ->develGenerateContentAddNodeTranslation($results, $node);
  }
}