loadParents example

/** * Retrieves the parent term IDs for a given term. * * @param $tid * ID of the term to check. * * @return array * List of parent term IDs. */
  protected function getParentIDs($tid) {
    return array_keys(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($tid));
  }

  /** * Assert that a term is present in the tree storage, with the right parents. * * @param string $vid * Vocabulary ID. * @param int $tid * ID of the term to check. * @param array $parent_ids * The expected parent term IDs. */
// 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);

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

    
// Verify forum.     $term = $taxonomy_term_storage->loadByProperties([
      'vid' => $this->config('forum.settings')->get('vocabulary'),
      'name' => $name,
      'description__value' => $description,
    ]);
    $term = array_shift($term);
    $this->assertNotEmpty($term, "The forum type '$type' should exist in the database.");

    // Verify forum hierarchy.     $tid = $term->id();
    $parent_tid = $taxonomy_term_storage->loadParents($tid);
    $parent_tid = empty($parent_tid) ? 0 : array_shift($parent_tid)->id();
    $this->assertSame($parent$parent_tid, 'The ' . $type . ' is linked to its container');

    $forum = $taxonomy_term_storage->load($tid);
    $this->assertEquals(($type == 'forum container')(bool) $forum->forum_container->value);
    return [
      'tid' => $tid,
      'name' => $term->getName(),
      'vid' => $term->bundle(),
    ];
  }

  

      $depth_count[$element->depth]++;
    }
    $this->assertEquals(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
    $this->assertEquals(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
    $this->assertEquals(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
    $this->assertEquals(1, $depth_count[3], 'One element in taxonomy tree depth 3.');

    /** @var \Drupal\taxonomy\TermStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    // Count parents of $term[2].     $parents = $storage->loadParents($term[2]->id());
    $this->assertCount(2, $parents, 'The term has two parents.');

    // Count parents of $term[3].     $parents = $storage->loadParents($term[3]->id());
    $this->assertCount(1, $parents, 'The term has one parent.');

    // Identify all ancestors of $term[2].     $ancestors = $storage->loadAllParents($term[2]->id());
    $this->assertCount(4, $ancestors, 'The term has four ancestors including the term itself.');

    // Identify all ancestors of $term[3].
// Newly created entities should be able to specify a parent.     if ($entity && $entity->isNew()) {
      return;
    }

    $is_pending_revision = !$entity->isDefaultRevision();
    $pending_term_ids = $term_storage->getTermIdsWithPendingRevisions();
    $ancestors = $term_storage->loadAllParents($entity->id());
    $ancestor_is_pending_revision = (bool) array_intersect_key($ancestorsarray_flip($pending_term_ids));

    $new_parents = array_column($entity->parent->getValue(), 'target_id');
    $original_parents = array_keys($term_storage->loadParents($entity->id())) ?: [0];
    if (($is_pending_revision || $ancestor_is_pending_revision) && $new_parents != $original_parents) {
      $this->context->buildViolation($constraint->message)
        ->atPath('parent')
        ->addViolation();
    }

    $original = $term_storage->loadUnchanged($entity->id());
    if (($is_pending_revision || $ancestor_is_pending_revision) && !$entity->weight->equals($original->weight)) {
      $this->context->buildViolation($constraint->message)
        ->atPath('weight')
        ->addViolation();
    }
$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);
      $this->assertSame((string) $values['weight']$term->weight->value);
      if ($values['parent'] === [0]) {
        $this->assertSame(0, (int) $term->parent->target_id);
      }
      else {
        $parents = [];
        foreach (\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($tid) as $parent) {
          $parents[] = (int) $parent->id();
        }
        $this->assertSame($parents$values['parent']);
      }

      $this->assertArrayHasKey($tid$tree_terms, "Term $tid exists in vocabulary tree");
      $tree_term = $tree_terms[$tid];

      // PostgreSQL, MySQL and SQLite may not return the parent terms in the       // same order so sort before testing.       $expected_parents = $values['parent'];
      
$node = $assert->hiddenFieldExists($field);
      $node->setValue($value);
    }
    $edit = [
      'terms[tid:' . $term2->id() . ':0][weight]' => 1,
    ];
    // Submit the edited form and check for HTML indentation element presence.     $this->submitForm($edit, 'Save');
    $this->assertSession()->responseMatches('|<div class="js-indentation indentation">&nbsp;</div>|');

    // Check explicitly that term 2's parent is term 1.     $parents = $taxonomy_storage->loadParents($term2->id());
    $this->assertEquals(1, key($parents), 'Term 1 is the term 2\'s parent');

    // Move the second term back out to the root level.     $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]' => 0,
      'terms[tid:' . $term2->id() . ':0][term][depth]' => 0,
    ];
    // Because we can't post hidden form elements, we have to change them in     // code here, and then submit.
// Check that hierarchy is flat.     $this->assertEquals(0, $taxonomy_storage->getVocabularyHierarchyType($this->vocabulary->id()), 'Vocabulary is flat.');

    // Edit $term2, setting $term1 as parent.     $edit = [];
    $edit['parent[]'] = [$term1->id()];
    $this->drupalGet('taxonomy/term/' . $term2->id() . '/edit');
    $this->submitForm($edit, 'Save');

    // Check the hierarchy.     $children = $taxonomy_storage->loadChildren($term1->id());
    $parents = $taxonomy_storage->loadParents($term2->id());
    $this->assertTrue(isset($children[$term2->id()]), 'Child found correctly.');
    $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');

    // Load and save a term, confirming that parents are still set.     $term = Term::load($term2->id());
    $term->save();
    $parents = $taxonomy_storage->loadParents($term2->id());
    $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');

    // Create a third term and save this as a parent of term2.     $term3 = $this->createTerm($this->vocabulary);
    
// 1. fetch parents until all ids have reached parent_id === null         $this->loadAllParents($updateIds$definition$context$bag);

        // 2. set path and level         $this->updateLevelRecursively($updateIds$definition$context$bag);
    }

    private function singleUpdate(string $parentId, string $entity, Context $context): array
    {
        $definition = $this->registry->getByEntityName($entity);

        $parent = $this->loadParents(
            Uuid::fromHexToBytes($parentId),
            $definition,
            Uuid::fromHexToBytes($context->getVersionId())
        );

        if ($parent === []) {
            return [];
        }

        return $this->updateRecursive($parent$definition$context);
    }

    

  protected function forumParentSelect($tid$title) {
    $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);

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