createTerm example

'bypass node access',
    ]));
    $this->vocabulary = $this->createVocabulary();
  }

  /** * Tests term indentation. */
  public function testTermIndentation() {
    $assert = $this->assertSession();
    // Create three taxonomy terms.     $term1 = $this->createTerm($this->vocabulary);
    $term2 = $this->createTerm($this->vocabulary);
    $term3 = $this->createTerm($this->vocabulary);

    // Get the taxonomy storage.     $taxonomy_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');

    // Indent the second term under the first one.     $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview');
    $hidden_edit = [
      'terms[tid:' . $term2->id() . ':0][term][tid]' => 2,
      'terms[tid:' . $term2->id() . ':0][term][parent]' => 1,
      
/** * Tests term argument transformation of hyphens and spaces. * * @dataProvider termArgumentTransformationProvider * * @param string $name * The name of the taxonomy term to use for the test. */
  public function testTermArgumentTransformation($name) {
    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $this->createTerm(['name' => $name]);

    /** @var \Drupal\views\ViewExecutable $view */
    $view = Views::getView('test_argument_transform_term');
    $view->initHandlers();

    /** @var string $hyphenated_term */
    $hyphenated_term = str_replace(' ', '-', $term->label());
    $this->assertTrue($view->argument['tid']->setArgument($hyphenated_term));
    // Assert hyphens are converted back to spaces.     $this->assertEquals($term->label()$view->argument['tid']->argument);
  }

  


  /** * Tests editing the parents of existing terms. */
  public function testEditingParents() {
    $terms = $this->doTestEditingSingleParent();
    $term_5 = array_pop($terms);
    $term_4 = array_pop($terms);

    // Create a term with multiple parents.     $term_6 = $this->createTerm('Test term 6', [
      // Term 5 comes before term 4 in the user interface, so add the parents in       // the matching order.       $term_5->id(),
      $term_4->id(),
    ]);
    $this->drupalGet($term_6->toUrl('edit-form'));
    $this->assertParentOption('<root>');
    $this->assertParentOption('Test term 1');
    $this->assertParentOption('-Test term 3');
    $this->assertParentOption('--Test term 5', TRUE);
    $this->assertParentOption('Test term 2');
    
/** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void {
    parent::setUp($import_test_views$modules);

    $this->enableViewsTestModule();

    /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
    $vocabulary = $this->createVocabulary();
    for ($i = 0; $i < 10; $i++) {
      $this->taxonomyTerms[] = $this->createTerm($vocabulary);
    }
    $this->taxonomyTerms[] = $this->createTerm($vocabulary['name' => '0' . $this->randomMachineName()]);
  }

  /** * Tests a taxonomy glossary view. */
  public function testTaxonomyGlossaryView() {
    $initials = [];
    foreach ($this->taxonomyTerms as $term) {
      $char = mb_strtolower(substr($term->label(), 0, 1));
      
'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

    // Create a time in the past for the archive.     $time = REQUEST_TIME - 3600;

    $this->addDefaultCommentField('node', 'page');

    for ($i = 0; $i <= 10; $i++) {
      $user = $this->drupalCreateUser();
      $term = $this->createTerm($vocabulary);

      $values = ['created' => $time, 'type' => 'page'];
      $values[$field_name][]['target_id'] = $term->id();

      // Make every other node promoted.       if ($i % 2) {
        $values['promote'] = TRUE;
      }
      $values['body'][]['value'] = Link::fromTextAndUrl('Node ' . 1, Url::fromRoute('entity.node.canonical', ['node' => 1]))->toString();

      $node = $this->drupalCreateNode($values);

      

  protected function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);

    $this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('user');
    $this->installConfig(['filter']);

    /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
    $vocabulary = $this->createVocabulary(['vid' => 'aaa']);
    $term = $this->createTerm($vocabulary);
    $this->terms[$term->id()] = $term;

    /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary2 */
    $vocabulary2 = $this->createVocabulary(['vid' => 'bbb']);
    $term = $this->createTerm($vocabulary2);
    $this->terms[$term->id()] = $term;

    // Create user 1 and set is as the logged in user, so that the logged in     // user has the correct permissions to view the vocabulary name.     $this->adminUser = User::create(['name' => $this->randomString()]);
    $this->adminUser->save();
    
CommentItemInterface::OPEN,
      $this->commentType->id()
    );
  }

  /** * Tests CSS classes on comments. */
  public function testEntityChanges() {
    $this->drupalLogin($this->webUser);
    // Create a new node.     $term = $this->createTerm($this->vocab, ['uid' => $this->webUser->id()]);

    // Add a comment.     /** @var \Drupal\comment\CommentInterface $comment */
    $comment = Comment::create([
      'entity_id' => $term->id(),
      'entity_type' => 'taxonomy_term',
      'field_name' => 'field_comment',
      'uid' => $this->webUser->id(),
      'status' => CommentInterface::PUBLISHED,
      'subject' => $this->randomMachineName(),
      'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      

  protected $view;

  /** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE, $modules = []): void {
    parent::setUp($import_test_views$modules);

    // Create a hierarchy 3 deep. Note the parent setup function creates two     // top-level terms w/o children.     $first = $this->createTerm(['name' => 'First']);
    $second = $this->createTerm(['name' => 'Second', 'parent' => $first->id()]);
    $third = $this->createTerm(['name' => 'Third', 'parent' => $second->id()]);

    // Create a node w/o any terms.     $settings = ['type' => 'article'];
    $this->nodes[] = $this->drupalCreateNode($settings);

    // Create a node with only the top level term.     $settings['field_views_testing_tags'][0]['target_id'] = $first->id();
    $this->nodes[] = $this->drupalCreateNode($settings);

    
/** * Tests that appropriate tags are added when querying the database. */
  public function testTaxonomyQueryAlter() {
    $this->installEntitySchema('taxonomy_term');

    // Create a new vocabulary and add a few terms to it.     $vocabulary = $this->createVocabulary();
    $terms = [];
    for ($i = 0; $i < 5; $i++) {
      $terms[$i] = $this->createTerm($vocabulary);
    }

    // Set up hierarchy. Term 2 is a child of 1.     $terms[2]->parent = $terms[1]->id();
    $terms[2]->save();

    /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
    $term_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');

    $this->setupQueryTagTestHooks();
    $loaded_term = $term_storage->load($terms[0]->id());
    
public function testParentHandlerSettings() {
    $vocabulary_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('taxonomy_term', $this->vocabulary->id());
    $parent_target_bundles = $vocabulary_fields['parent']->getSetting('handler_settings')['target_bundles'];
    $this->assertSame([$this->vocabulary->id() => $this->vocabulary->id()]$parent_target_bundles);
  }

  /** * Tests terms in a single and multiple hierarchy. */
  public function testTaxonomyTermHierarchy() {
    // Create two taxonomy terms.     $term1 = $this->createTerm($this->vocabulary);
    $term2 = $this->createTerm($this->vocabulary);

    // Get the taxonomy storage.     /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
    $taxonomy_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');

    // Check that hierarchy is flat.     $this->assertEquals(0, $taxonomy_storage->getVocabularyHierarchyType($this->vocabulary->id()), 'Vocabulary is flat.');

    // Edit $term2, setting $term1 as parent.     $edit = [];
    

  public static $testViews = ['test_argument_validator_term'];

  /** * {@inheritdoc} */
  protected function setUp($import_test_views = TRUE): void {
    parent::setUp($import_test_views);

    // Add three terms to the 'tags' vocabulary.     for ($i = 0; $i < 3; $i++) {
      $this->terms[] = $term = $this->createTerm();
      $this->names[] = $term->label();
      $this->ids[] = $term->id();
    }
  }

  /** * Tests the term argument validator plugin. */
  public function testArgumentValidatorTerm() {
    $view = Views::getView('test_argument_validator_term');
    $view->initHandlers();

    
/** * Tests that the pager is displayed properly on the term overview page. */
  public function testTaxonomyTermOverviewPager() {
    // Set limit to 3 terms per page.     $this->config('taxonomy.settings')
      ->set('terms_per_page_admin', '3')
      ->save();

    // Create 3 terms.     for ($x = 1; $x <= 3; $x++) {
      $this->createTerm($this->vocabulary);
    }

    // Get Page 1.     $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
    // Pager should not be visible.     $this->assertSession()->responseNotMatches('|<nav class="pager" [^>]*>|');

    // Create 3 more terms to show pager.     for ($x = 1; $x <= 3; $x++) {
      $this->createTerm($this->vocabulary);
    }

    
/** * Tests loading multiple taxonomy terms by term ID and vocabulary. */
  public function testTaxonomyTermMultipleLoad() {
    // Create a vocabulary.     $vocabulary = $this->createVocabulary();

    // Create five terms in the vocabulary.     $i = 0;
    while ($i < 5) {
      $i++;
      $this->createTerm($vocabulary);
    }
    // Load the terms from the vocabulary.     $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $terms = $term_storage->loadByProperties(['vid' => $vocabulary->id()]);
    $count = count($terms);
    $this->assertEquals(5, $countnew FormattableMarkup('Correct number of terms were loaded. @count terms.', ['@count' => $count]));

    // Load the same terms again by tid.     $terms2 = Term::loadMultiple(array_keys($terms));
    $this->assertEquals($terms$terms2, 'Both arrays contain the same terms.');

    
$this->assertSession()->pageTextContains('translatedTwo');
    $this->assertSession()->pageTextContains('translatedThree');
  }

  /** * Setup translated terms in a hierarchy. */
  protected function setUpTerms() {
    $parent_vid = 0;
    foreach ($this->termTranslationMap as $name => $translation) {

      $term = $this->createTerm($this->vocabulary, [
        'name' => $name,
        'langcode' => $this->baseLangcode,
        'parent' => $parent_vid,
      ]);

      $term->addTranslation($this->translateToLangcode, [
        'name' => $translation,
      ]);
      $term->save();

      // Each term is nested under the last.
->setComponent($this->fieldName1, [
        'type' => 'entity_reference_label',
      ])
      ->save();
  }

  /** * Tests that the taxonomy term view is working properly. */
  public function testTaxonomyTermView() {
    // Create terms in the vocabulary.     $term = $this->createTerm();

    // Post an article.     $edit = [];
    $edit['title[0][value]'] = $original_title = $this->randomMachineName();
    $edit['body[0][value]'] = $this->randomMachineName();
    $edit["{$this->fieldName1}[]"] = $term->id();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);

    $this->drupalGet('taxonomy/term/' . $term->id());
    
Home | Imprint | This part of the site doesn't use cookies.