loadAllParents example

/** @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].     $ancestors = $storage->loadAllParents($term[3]->id());
    $this->assertCount(5, $ancestors, 'The term has five ancestors including the term itself.');
  }

  /** * Tests that a Term is renderable when unsaved (preview). */
  public function testTermPreview() {
    
public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    $term = $route_match->getParameter('taxonomy_term');
    // Breadcrumb needs to have terms cacheable metadata as a cacheable     // dependency even though it is not shown in the breadcrumb because e.g. its     // parent might have changed.     $breadcrumb->addCacheableDependency($term);
    // @todo This overrides any other possible breadcrumb and is a pure     // hard-coded presumption. Make this behavior configurable per     // vocabulary or term.     $parents = $this->entityTypeManager->getStorage('taxonomy_term')->loadAllParents($term->id());
    // Remove current term being accessed.     array_shift($parents);
    foreach (array_reverse($parents) as $term) {
      $term = $this->entityRepository->getTranslationFromContext($term);
      $breadcrumb->addCacheableDependency($term);
      $breadcrumb->addLink(Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]));
    }

    // This breadcrumb builder is based on a route parameter, and hence it     // depends on the 'route' cache context.     $breadcrumb->addCacheContexts(['route']);

    
&& $route_match->getParameter('node')
      && $this->forumManager->checkNodeType($route_match->getParameter('node'));
  }

  /** * {@inheritdoc} */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = parent::build($route_match);
    $breadcrumb->addCacheContexts(['route']);

    $parents = $this->termStorage->loadAllParents($route_match->getParameter('node')->forum_tid);
    if ($parents) {
      $parents = array_reverse($parents);
      foreach ($parents as $parent) {
        $breadcrumb->addCacheableDependency($parent);
        $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page',
          [
            'taxonomy_term' => $parent->id(),
          ]
        ));
      }
    }

    

  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = parent::build($route_match);
    $breadcrumb->addCacheContexts(['route']);

    // Add all parent forums to breadcrumbs.     /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $route_match->getParameter('taxonomy_term');
    $term_id = $term->id();
    $breadcrumb->addCacheableDependency($term);

    $parents = $this->termStorage->loadAllParents($term_id);
    if ($parents) {
      foreach (array_reverse($parents) as $parent) {
        if ($parent->id() != $term_id) {
          $breadcrumb->addCacheableDependency($parent);
          $breadcrumb->addLink(Link::createFromRoute($parent->label(), 'forum.page', [
            'taxonomy_term' => $parent->id(),
          ]));
        }
      }
    }

    

  public function forumPage(TermInterface $taxonomy_term) {
    // Get forum details.     $taxonomy_term->forums = $this->forumManager->getChildren($this->config('forum.settings')->get('vocabulary')$taxonomy_term->id());
    $taxonomy_term->parents = $this->termStorage->loadAllParents($taxonomy_term->id());

    if (empty($taxonomy_term->forum_container->value)) {
      $build = $this->forumManager->getTopics($taxonomy_term->id()$this->currentUser());
      $topics = $build['topics'];
      $header = $build['header'];
    }
    else {
      $topics = [];
      $header = [];
    }
    return $this->build($taxonomy_term->forums, $taxonomy_term$topics$taxonomy_term->parents, $header);
  }
public function validate($entity, Constraint $constraint) {
    $term_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
    assert($term_storage instanceof TermStorageInterface);

    // 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());
    
// the batch update does not support versioning, so fallback to single updates         if ($definition->isVersionAware() && $context->getVersionId() !== Defaults::LIVE_VERSION) {
            foreach ($updateIds as $id) {
                $this->singleUpdate(Uuid::fromBytesToHex($id)$entity$context);
            }

            return;
        }

        // 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),
            
Home | Imprint | This part of the site doesn't use cookies.