You are here

function drush_workbench_access_test in Workbench Access 8

Same name and namespace in other branches
  1. 7 workbench_access.drush.inc \drush_workbench_access_test()

Installs the test vocabulary.

@TODO: Refactor

File

./workbench_access.drush.inc, line 46
Drush commands for Workbench Access.

Code

function drush_workbench_access_test() {
  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) {
    drush_print('The test vocabulary has already been created.');
  }
}