You are here

public function WorkbenchAccessCommands::installTest in Workbench Access 8

Installs the workbench_access test vocabulary.

@command workbench_access:installTest @aliases wa-test

File

src/Commands/WorkbenchAccessCommands.php, line 22

Class

WorkbenchAccessCommands
A Drush commandfile for Workbench Access.

Namespace

Drupal\workbench_access\Commands

Code

public function installTest() {
  try {

    // Create a vocabulary.
    $vocabulary = Vocabulary::create([
      'name' => 'Workbench Access',
      'description' => 'Test taxonomy for Workbench Access',
      'vid' => 'workbench_access',
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'weight' => 100,
    ]);
    $vocabulary
      ->save();

    // Create some terms.
    $terms = [
      'Alumni',
      'Faculty',
      'Staff',
      'Students',
    ];
    $children = [
      'Directory',
      'Information',
    ];
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);
    foreach ($terms as $name) {
      $term = Term::create([
        'name' => $name,
        'description' => [],
        'vid' => $vocabulary
          ->id(),
        'parent' => 0,
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ]);
      $term
        ->save();
      foreach ($children as $child) {
        $child = Term::create([
          'name' => "{$name} {$child}",
          'description' => [],
          'vid' => $vocabulary
            ->id(),
          'parent' => $term
            ->id(),
          'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        ]);
        $child
          ->save();
      }
    }
  } catch (Exception $e) {
    $this
      ->logger()
      ->warning(dt('The test vocabulary has already been created.'));
  }
  $this
    ->logger()
    ->success(dt('Workbench Access test vocabulary created.'));
}