You are here

public function CreateFixtureNodesForTestingCommands::createNodesWithPermissions in Permissions by Term 8.2

@command permissions-by-term:create-nodes-with-permissions @aliases pbtcnwp

File

src/Commands/CreateFixtureNodesForTestingCommands.php, line 36

Class

CreateFixtureNodesForTestingCommands
A Drush commandfile.

Namespace

Drupal\permissions_by_term\Commands

Code

public function createNodesWithPermissions(int $numNodes = 1000) : void {
  if ($this
    ->io()
    ->confirm('Do you really want to create ' . $numNodes . ' Drupal nodes with permissions? This is a testing feature for the permissions by term module.', FALSE)) {
    $this
      ->io()
      ->progressStart($numNodes);
    for ($i = 0; $i <= $numNodes; ++$i) {
      $term = Term::create([
        'name' => $this->random
          ->word(10),
        'vid' => 'tags',
      ]);
      $term
        ->save();
      $this->accessStorage
        ->addTermPermissionsByUserIds([
        1,
      ], $term
        ->id());
      $node = Node::create([
        'type' => 'article',
        'title' => $this->random
          ->word(10),
        'field_tags' => [
          [
            'target_id' => $term
              ->id(),
          ],
        ],
      ]);
      $node
        ->save();
      $this
        ->io()
        ->progressAdvance();
    }
    $this
      ->io()
      ->progressFinish();
  }
}