You are here

class CreateFixtureNodesForTestingCommands in Permissions by Term 8.2

A Drush commandfile.

Hierarchy

Expanded class hierarchy of CreateFixtureNodesForTestingCommands

1 string reference to 'CreateFixtureNodesForTestingCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses CreateFixtureNodesForTestingCommands
permissions_by_term.create_fixture_nodes_for_testing_commands in ./drush.services.yml
\Drupal\permissions_by_term\Commands\CreateFixtureNodesForTestingCommands

File

src/Commands/CreateFixtureNodesForTestingCommands.php, line 15

Namespace

Drupal\permissions_by_term\Commands
View source
class CreateFixtureNodesForTestingCommands extends DrushCommands {

  /**
   * @var \Drupal\permissions_by_term\Service\AccessStorage
   */
  private $accessStorage;

  /**
   * @var \Drupal\Component\Utility\Random
   */
  private $random;
  public function __construct(AccessStorage $accessStorage) {
    $this->accessStorage = $accessStorage;
    $this->random = new Random();
  }

  /**
   * @command permissions-by-term:create-nodes-with-permissions
   * @aliases pbtcnwp
   */
  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();
    }
  }

}

Members