Repository: pimcore/pimcore (language: php)

Issue:
[Bug]: HardLink Child-Resolver broken
### Pimcore version

11.x

### Steps to reproduce

```bash
- Create an Hardlink
- call $hardLink->getChildren()
```

There are two issues:

### I. An iterator cannot be used with foreach by reference

This exception will rise because of this:
https://github.com/pimcore/pimcore/blob/3a49548df10520b68ea9fcdab8ba3fed8697dff8/models/Document/Hardlink.php#L178-L181

I fixed it by rewriting it like this:

```php
$wrappedSourceChildren = [];
if ($this->getChildrenFromSource() && $this->getSourceDocument() && !\Pimcore::inAdmin()) {
    $sourceChildren = $this->getSourceDocument()->getChildren($includingUnpublished)->getDocuments();
    foreach ($sourceChildren as $key => $c) {
        $wrappedChild = Document\Hardlink\Service::wrap($c);
        $wrappedChild->setHardLinkSource($this);
        $wrappedChild->setPath(preg_replace('@^' . preg_quote($this->getSourceDocument()->getRealFullPath(), '@') . '@', $this->getRealFullPath(), $c->getRealPath()));
        $wrappedSourceChildren[$key] = $wrappedChild;
    }
}

$children->setData(array_merge($wrappedSourceChildren, $children->load()));
```
### II. Child Fetch
Secondly, the children (now available and set via `$children->setData()`) won't be available for long because of this:
https://github.com/pimcore/pimcore/blob/04e63bc7f7d434dc17f60ba3a61c1ef6f4d15862/lib/Navigation/Builder.php#L324-L326

That's because the `load()` method will execute the original listing query.

I was able to fix it by checking the parent document instance:

```php
if (
  $parentDocument instanceof Document\Hardlink || 
  $parentDocument instanceof Document\Hardlink\Wrapper\WrapperInterface
) {
    return $parentDocument->getChildren()->getData();
}

return $parentDocument->getChildren()->load();
```

Not sure if this is the correct approach. If so, I'll provide an PR for both changes.

### Actual Behavior

See above

### Expected Behavior

A working hardlink context.


Discussion / hints:
any updates on this @dvesh3? 
Hey @solverat the changes you suggested looks good to me. Could you please provide a PR for 11.0? thanks!
@dvesh3 PR incoming! :)

Fix patch:
diff --git a/lib/Navigation/Builder.php b/lib/Navigation/Builder.php
index 98267707a72..cca258f5b2f 100644
--- a/lib/Navigation/Builder.php
+++ b/lib/Navigation/Builder.php
@@ -332,6 +332,10 @@ protected function getChildren(Document $parentDocument): array
     {
         // the intention of this function is mainly to be overridden in order to customize the behavior of the navigation
         // e.g. for custom filtering and other very specific use-cases
+        if ($parentDocument instanceof Document\Hardlink || $parentDocument instanceof Document\Hardlink\Wrapper\WrapperInterface) {
+            return $parentDocument->getChildren()->getData();
+        }
+
         return $parentDocument->getChildren()->load();
     }
 
diff --git a/models/Document/Hardlink.php b/models/Document/Hardlink.php
index 011c6272a18..28a49d1e1ba 100644
--- a/models/Document/Hardlink.php
+++ b/models/Document/Hardlink.php
@@ -172,17 +172,18 @@ public function getChildren(bool $includingUnpublished = false): Listing
         if (!isset($this->children[$cacheKey])) {
             $children = parent::getChildren($includingUnpublished);
 
-            $sourceChildren = [];
+            $wrappedSourceChildren = [];
             if ($this->getChildrenFromSource() && $this->getSourceDocument() && !\Pimcore::inAdmin()) {
-                $sourceChildren = $this->getSourceDocument()->getChildren($includingUnpublished);
-                foreach ($sourceChildren as &$c) {
-                    $c = Document\Hardlink\Service::wrap($c);
-                    $c->setHardLinkSource($this);
-                    $c->setPath(preg_replace('@^' . preg_quote($this->getSourceDocument()->getRealFullPath(), '@') . '@', $this->getRealFullPath(), $c->getRealPath()));
+                $sourceChildren = $this->getSourceDocument()->getChildren($includingUnpublished)->getDocuments();
+                foreach ($sourceChildren as $key => $c) {
+                    $wrappedChild = Document\Hardlink\Service::wrap($c);
+                    $wrappedChild->setHardLinkSource($this);
+                    $wrappedChild->setPath(preg_replace('@^' . preg_quote($this->getSourceDocument()->getRealFullPath(), '@') . '@', $this->getRealFullPath(), $c->getRealPath()));
+                    $wrappedSourceChildren[$key] = $wrappedChild;
                 }
             }
 
-            $children->setData(array_merge($sourceChildren, $children->load()));
+            $children->setData(array_merge($wrappedSourceChildren, $children->load()));
 
             $this->setChildren($children, $includingUnpublished);
         }


Test patch: