getLevel example

$categoryC = $this->createCategory($categoryA);

        $categoryD = $this->createCategory($categoryC);

        $categories = $this->categoryRepository->search(new Criteria([$categoryA$categoryB$categoryC$categoryD])$this->context);

        static::assertNull($categories->get($categoryA)->getPath());
        static::assertEquals("|{$categoryA}|", $categories->get($categoryB)->getPath());
        static::assertEquals("|{$categoryA}|", $categories->get($categoryC)->getPath());
        static::assertEquals("|{$categoryA}|{$categoryC}|", $categories->get($categoryD)->getPath());

        static::assertEquals(1, $categories->get($categoryA)->getLevel());
        static::assertEquals(2, $categories->get($categoryB)->getLevel());
        static::assertEquals(2, $categories->get($categoryC)->getLevel());
        static::assertEquals(3, $categories->get($categoryD)->getLevel());

        $this->categoryRepository->update([[
            'id' => $categoryD,
            'parentId' => $categoryA,
        ]]$this->context);

        /* Category A ├── Category B ├── Category C ├── Category D */
public function getPersistent(): self
    {
        return $this->filter(fn (Error $error) => $error->isPersistent());
    }

    /** * @return array<array-key, Error|null> */
    public function filterByErrorLevel(int $errorLevel): array
    {
        return $this->fmap(static fn (Error $error): ?Error => $errorLevel === $error->getLevel() ? $error : null);
    }

    public function hasLevel(int $errorLevel): bool
    {
        foreach ($this->getIterator() as $element) {
            if ($element->getLevel() === $errorLevel) {
                return true;
            }
        }

        return false;
    }
$criteria = new Criteria([$parent]);
        $criteria->addAssociation('children');

        /** @var CategoryCollection $result */
        $result = $this->repository->search($criteria, Context::createDefaultContext());

        /** @var CategoryEntity $first */
        $first = $result->first();

        // First Level Category should have Level 1         static::assertEquals($parent$first->getId());
        static::assertEquals(1, $first->getLevel());

        // Second Level Categories should have Level 2         /** @var CategoryCollection $children */
        $children = $first->getChildren();
        $children->sortByPosition();
        $childrenArray = array_values($children->getElements());
        static::assertEquals($recordA$childrenArray[0]->getId());
        static::assertEquals(2, $childrenArray[0]->getLevel());
        static::assertEquals($recordB$childrenArray[1]->getId());
        static::assertEquals(2, $childrenArray[1]->getLevel());

        

        return true;
    }

    /** * @return array<string, mixed> */
    public function jsonSerialize(): array
    {
        $data = get_object_vars($this);
        $data['key'] = $this->getId();
        $data['level'] = $this->getLevel();
        $data['message'] = $this->getMessage();
        $data['messageKey'] = $this->getMessageKey();
        unset($data['file']$data['line']);

        return $data;
    }
}


  /** * Reacts to map message event. * * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event * The migration event. * @param string $name * The event name. */
  public function mapMessageRecorder(MigrateIdMapMessageEvent $event$name) {
    if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE ||
        $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
      $type = 'status';
    }
    else {
      $type = 'error';
    }
    $source_id_string = implode(',', $event->getSourceIdValues());
    $this->display($source_id_string . ': ' . $event->getMessage()$type);
  }

  /** * {@inheritdoc} */
public static function onMapDelete(MigrateMapDeleteEvent $event) {
    static::$numProcessed++;
  }

  /** * Displays any messages being logged to the ID map. * * @param \Drupal\migrate\Event\MigrateIdMapMessageEvent $event * The message event. */
  public static function onIdMapMessage(MigrateIdMapMessageEvent $event) {
    if ($event->getLevel() == MigrationInterface::MESSAGE_NOTICE || $event->getLevel() == MigrationInterface::MESSAGE_INFORMATIONAL) {
      $type = 'status';
    }
    else {
      $type = 'error';
    }
    $source_id_string = implode(',', $event->getSourceIdValues());
    $message = t('Source ID @source_id: @message', ['@source_id' => $source_id_string, '@message' => $event->getMessage()]);
    static::$messages->display($message$type);
  }

}
// Get the process pipeline.     $pipeline = FALSE;
    if ($source->valid()) {
      try {
        $pipeline = $this->migration->getProcessPlugins();
      }
      catch (MigrateException $e) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();
        $this->getIdMap()->saveIdMapping($row[]$e->getStatus());
        $this->saveMessage($e->getMessage()$e->getLevel());
      }
    }

    $return = MigrationInterface::RESULT_COMPLETED;
    if ($pipeline) {
      $id_map = $this->getIdMap();
      $destination = $this->migration->getDestinationPlugin();
      while ($source->valid()) {
        $row = $source->current();
        $this->sourceIdValues = $row->getSourceIdValues();

        
/** * @param int $level * * @return int */
    public function getLevel($level = 0)
    {
        $parent = $this->getParent();

        if ($parent) {
            $level = $parent->getLevel($level + 1);
        }

        return $level;
    }

    /** * @param ArrayCollection<Category> $children * * @return Category */
    public function setChildren($children)
    {
static::assertTrue($facade->has('notice'));

        $facade->warning('duplicate');
        $facade->warning('duplicate');
        static::assertTrue($facade->has('duplicate'));
        static::assertCount(4, $facade);
        $facade->remove('duplicate');
        static::assertFalse($facade->has('duplicate'));
        static::assertCount(3, $facade);

        static::assertInstanceOf(Error::class$facade->get('error'));
        static::assertEquals(Error::LEVEL_ERROR, $facade->get('error')->getLevel());
        static::assertInstanceOf(Error::class$facade->get('warning'));
        static::assertEquals(Error::LEVEL_WARNING, $facade->get('warning')->getLevel());
        static::assertInstanceOf(Error::class$facade->get('notice'));
        static::assertEquals(Error::LEVEL_NOTICE, $facade->get('notice')->getLevel());
    }
}

    public function sCategoryPath($categoryId)
    {
        $parts = $this->modelManager->getRepository(Category::class)->getPathById($categoryId);
        if (\is_string($parts)) {
            return [$parts];
        }
        $level = Shopware()->Shop()->getCategory()->getLevel() ?: 1;

        return \array_slice($parts$level);
    }

    /** * @deprecated in 5.6, will be removed in 5.7 without replacement * * @return Smarty_Data */
    public function getData()
    {
        
Home | Imprint | This part of the site doesn't use cookies.