You are here

public function PermissionsByTermContext::createTerms in Permissions by Term 8

Creates one or more terms on an existing vocabulary.

Provide term data in the following format:

| name | parent | description | weight | taxonomy_field_image | access_user | access_role | | Snook | Fish | Marine fish | 10 | snook-123.jpg | Bob | editor | | ... | ... | ... | ... | ... | ... | ... |

Only the 'name' field is required.

@Given restricted :vocabulary terms:

File

tests/src/Behat/Context/PermissionsByTermContext.php, line 40

Class

PermissionsByTermContext
Class PermissionsByTermContext

Namespace

Drupal\Tests\permissions_by_term\Behat\Context

Code

public function createTerms($vocabulary, TableNode $termsTable) {
  foreach ($termsTable
    ->getHash() as $termsHash) {
    $term = (object) $termsHash;
    $term->vocabulary_machine_name = $vocabulary;
    $this
      ->termCreate($term);
    $accessStorage = \Drupal::Service('permissions_by_term.access_storage');
    if (!empty($termsHash['access_user'])) {
      $userNames = explode(', ', $termsHash['access_user']);
      foreach ($userNames as $userName) {
        $accessStorage
          ->addTermPermissionsByUserIds([
          $accessStorage
            ->getUserIdByName($userName)['uid'],
        ], $term->tid);
      }
    }
    if (!empty($termsHash['access_role'])) {
      $rolesIds = explode(', ', $termsHash['access_role']);
      $accessStorage
        ->addTermPermissionsByRoleIds($rolesIds, $term->tid);
    }
  }
}