function ContentDevelGenerate::develGenerateContentPreNode

Same name and namespace in other branches
  1. 4.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::develGenerateContentPreNode()

Preprocesses $results before adding content.

Parameters

array $results: Results information.

3 calls to ContentDevelGenerate::develGenerateContentPreNode()
ContentDevelGenerate::batchContentPreNode in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Batch wrapper for calling ContentPreNode.
ContentDevelGenerate::generateContent in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Generate content when not in batch mode.
ContentDevelGenerate::validateDrushParams in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Responsible for validating Drush params.

File

devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php, line 598

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function develGenerateContentPreNode(array &$results) : void {
  $authors = $results['authors'];
  // Remove non-selected users. !== 0 will leave the Anonymous user in if it
  // was selected on the form or entered in the drush parameters.
  $authors = array_filter($authors, static fn($k): bool => $k !== 0);
  // Likewise remove non-selected roles.
  $roles = $results['roles'];
  $roles = array_filter($roles, static fn($k): bool => $k !== 0);
  // If specific roles have been selected then also add up to 50 users who
  // have one of these roles. There is no direct way randomise the selection
  // using entity queries, so we use a database query instead.
  if ($roles !== [] && !in_array('authenticated', $roles)) {
    $query = $this->database
      ->select('user__roles', 'ur')
      ->fields('ur', [
      'entity_id',
      'roles_target_id',
    ])
      ->condition('roles_target_id', $roles, 'in')
      ->range(0, 50)
      ->orderRandom();
    $uids = array_unique($query->execute()
      ->fetchCol());
    // If the 'anonymous' role is selected, then add '0' to the user ids. Also
    // do this if no users were specified and none were found with the role(s)
    // requested. This makes it clear that no users were found. It would be
    // worse to fall through and select completely random users who do not
    // have any of the roles requested.
    if (in_array('anonymous', $roles) || $authors === [] && $uids === []) {
      $uids[] = '0';
    }
    $authors = array_unique(array_merge($authors, $uids));
  }
  // If still no authors have been collected, or the 'authenticated' role was
  // requested then add a random set of users up to a maximum of 50.
  if ($authors === [] || in_array('authenticated', $roles)) {
    $query = $this->database
      ->select('users', 'u')
      ->fields('u', [
      'uid',
    ])
      ->range(0, 50)
      ->orderRandom();
    $uids = $query->execute()
      ->fetchCol();
    $authors = array_unique(array_merge($authors, $uids));
  }
  $results['users'] = $authors;
}