loadTree example

    $edit[$field->getName() . '[target_id]'] = Tags::implode($terms);

    // Verify the placeholder is there.     $this->drupalGet('node/add/article');
    $this->assertSession()->responseContains('placeholder="Start typing here."');

    // Preview and verify the terms appear but are not created.     $this->submitForm($edit, 'Preview');
    foreach ($terms as $term) {
      $this->assertSession()->pageTextContains($term);
    }
    $tree = $this->container->get('entity_type.manager')->getStorage('taxonomy_term')->loadTree($this->vocabulary->id());
    $this->assertEmpty($tree, 'The terms are not created on preview.');

    // Save, creating the terms.     $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('Article ' . $edit['title[0][value]'] . ' has been created.');

    // Verify that the creation message contains a link to a node.     $this->assertSession()->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "node/")]');

    foreach ($terms as $term) {
      

  protected function assertHierarchy(string $vid, int $tid, array $parent_ids): void {
    if (!isset($this->treeData[$vid])) {
      $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
      $this->treeData[$vid] = [];
      foreach ($tree as $item) {
        $this->treeData[$vid][$item->tid] = $item;
      }
    }

    $this->assertArrayHasKey($tid$this->treeData[$vid], "Term $tid exists in taxonomy tree");
    $term = $this->treeData[$vid][$tid];
    $this->assertEquals($parent_idsarray_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
  }

}
    // deleted.     $dev->delete();
    $stage->delete();
    $this->assertNull(Workspace::load('dev'));
    $this->assertNull(Workspace::load('stage'));
  }

  /** * Tests loading the workspace tree when there are no workspaces available. */
  public function testEmptyWorkspaceTree() {
    $tree = \Drupal::service('workspaces.repository')->loadTree();
    $this->assertSame([]$tree);
  }

}
$container->get('workspaces.manager'),
      $container->get('workspaces.repository'),
      $container->get('renderer')
    );
  }

  /** * {@inheritdoc} */
  public function load() {
    // Get all the workspace entities and sort them in tree order.     $workspace_tree = $this->workspaceRepository->loadTree();
    $entities = array_replace($workspace_tree$this->storage->loadMultiple());
    foreach ($entities as $id => $entity) {
      $entity->_depth = $workspace_tree[$id]['depth'];
    }

    return $entities;
  }

  /** * {@inheritdoc} */
  

  protected function assertHierarchy(string $vid, int $tid, array $parent_ids): void {
    if (!isset($this->treeData[$vid])) {
      $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
      $this->treeData[$vid] = [];
      foreach ($tree as $item) {
        $this->treeData[$vid][$item->tid] = $item;
      }
    }

    $this->assertArrayHasKey($tid$this->treeData[$vid], "Term $tid exists in taxonomy tree");
    $term = $this->treeData[$vid][$tid];
    $this->assertEquals($parent_idsarray_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
  }

  
// Terms from previous and next pages are shown if the term tree would have     // been cut in the middle. Keep track of how many extra terms we show on     // each page of terms.     $back_step = NULL;
    $forward_step = 0;

    // An array of the terms to be displayed on this page.     $current_page = [];

    $delta = 0;
    $term_deltas = [];
    $tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
    $tree_index = 0;
    $complete_tree = NULL;
    do {
      // In case this tree is completely empty.       if (empty($tree[$tree_index])) {
        break;
      }
      $delta++;
      // Count entries before the current page.       if ($page && ($page * $page_increment) > $before_entries && !isset($back_step)) {
        $before_entries++;
        


  /** * Tests the forum node pager for nodes with multiple grants per realm. */
  public function testForumPager() {
    // Look up the forums vocabulary ID.     $vid = $this->config('forum.settings')->get('vocabulary');
    $this->assertNotEmpty($vid, 'Forum navigation vocabulary ID is set.');

    // Look up the general discussion term.     $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
    $tid = reset($tree)->tid;
    $this->assertNotEmpty($tid, 'General discussion term is found in the forum vocabulary.');

    // Create 30 nodes.     for ($i = 0; $i < 30; $i++) {
      $this->drupalCreateNode([
        'nid' => NULL,
        'type' => 'forum',
        'taxonomy_forums' => [
          ['target_id' => $tid],
        ],
      ]);

  protected function assertHierarchy(string $vid, int $tid, array $parent_ids): void {
    if (!isset($this->treeData[$vid])) {
      $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
      $this->treeData[$vid] = [];
      foreach ($tree as $item) {
        $this->treeData[$vid][$item->tid] = $item;
      }
    }

    $this->assertArrayHasKey($tid$this->treeData[$vid], "Term $tid exists in taxonomy tree");
    $term = $this->treeData[$vid][$tid];
    // PostgreSQL, MySQL and SQLite may not return the parent terms in the same     // order so sort before testing.     sort($parent_ids);
    
'weight' => 8,
        'parent' => [4, 5],
      ],
    ];
    $terms = Term::loadMultiple(array_keys($expected_results));

    // Find each term in the tree.     $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $vids = array_unique(array_column($expected_results, 'vid'));
    $tree_terms = [];
    foreach ($vids as $vid) {
      foreach ($storage->loadTree($vid) as $term) {
        $tree_terms[$term->tid] = $term;
      }
    }

    foreach ($expected_results as $tid => $values) {
      /** @var \Drupal\taxonomy\Entity\Term $term */
      $term = $terms[$tid];
      $language = isset($values['language']) ? $values['language'] . ' - ' : '';
      $this->assertSame("{$language}term {$tid} of vocabulary {$values['source_vid']}", $term->name->value);
      $this->assertSame("{$language}description of term {$tid} of vocabulary {$values['source_vid']}", $term->description->value);
      $this->assertSame($values['vid']$term->vid->target_id);
      
$taxonomy_storage = $this->entityTypeManager->getStorage('taxonomy_term');
    $parents = $taxonomy_storage->loadParents($tid);
    if ($parents) {
      $parent = array_shift($parents);
      $parent = $parent->id();
    }
    else {
      $parent = 0;
    }

    $vid = $this->config('forum.settings')->get('vocabulary');
    $children = $taxonomy_storage->loadTree($vid$tid, NULL, TRUE);

    // A term can't be the child of itself, nor of its children.     foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
    $exclude[] = $tid;

    $tree = $taxonomy_storage->loadTree($vid, 0, NULL, TRUE);
    $options[0] = '<' . $this->t('root') . '>';
    if ($tree) {
      foreach ($tree as $term) {
        
return $form;

  }

  /** * {@inheritdoc} */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    // Get all the workspace entities and sort them in tree order.     $storage = $this->entityTypeManager->getStorage('workspace');
    $workspace_tree = $this->workspaceRepository->loadTree();
    $entities = array_replace($workspace_tree$storage->loadMultiple());

    // If we need to restrict the list of workspaces by searching only a part of     // their label ($match) or by a number of results ($limit), the workspace     // tree would be mangled because it wouldn't contain all the tree items.     if ($match || $limit) {
      $options = parent::getReferenceableEntities($match$match_operator$limit);
    }
    else {
      $options = [];
      foreach ($entities as $entity) {
        

  protected function assertHierarchy(string $vid, int $tid, array $parent_ids): void {
    if (!isset($this->treeData[$vid])) {
      $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
      $this->treeData[$vid] = [];
      foreach ($tree as $item) {
        $this->treeData[$vid][$item->tid] = $item;
      }
    }

    $this->assertArrayHasKey($tid$this->treeData[$vid], "Term $tid exists in taxonomy tree");
    $term = $this->treeData[$vid][$tid];
    $this->assertEquals($parent_idsarray_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
  }

  

  public function hasParent() {
    return !$this->get('parent')->isEmpty();
  }

  /** * {@inheritdoc} */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage$entities);

    $workspace_tree = \Drupal::service('workspaces.repository')->loadTree();

    // Ensure that workspaces that have descendants can not be deleted.     foreach ($entities as $entity) {
      if (!empty($workspace_tree[$entity->id()]['descendants'])) {
        throw new \InvalidArgumentException("The {$entity->label()} workspace can not be deleted because it has child workspaces.");
      }
    }
  }

  /** * {@inheritdoc} */
/** @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());
    // First term was loaded.     $this->assertEquals($terms[0]->id()$loaded_term->id());
    // TermStorage::load().     $this->assertQueryTagTestResult(1, 0);

    $this->setupQueryTagTestHooks();
    $loaded_terms = $term_storage->loadTree($vocabulary->id());
    // All terms were loaded.     $this->assertCount(5, $loaded_terms);
    // TermStorage::loadTree().     $this->assertQueryTagTestResult(1, 1);

    $this->setupQueryTagTestHooks();
    $loaded_terms = $term_storage->loadParents($terms[2]->id());
    // All parent terms were loaded.     $this->assertCount(1, $loaded_terms);
    // TermStorage::loadParents().     $this->assertQueryTagTestResult(3, 1);

    
      // deleted.       $this->cache->set('workspace_tree', $this->tree, Cache::PERMANENT, $this->entityTypeManager->getDefinition('workspace')->getListCacheTags());
    }

    return $this->tree;
  }

  /** * {@inheritdoc} */
  public function getDescendantsAndSelf($workspace_id) {
    return array_merge([$workspace_id]$this->loadTree()[$workspace_id]['descendants']);
  }

  /** * {@inheritdoc} */
  public function resetCache() {
    $this->cache->invalidate('workspace_tree');
    $this->tree = NULL;

    return $this;
  }

}
Home | Imprint | This part of the site doesn't use cookies.