]> BookStack Code Mirror - bookstack/blobdiff - app/Api/ApiEntityListFormatter.php
Updated translations with latest Crowdin changes (#5695)
[bookstack] / app / Api / ApiEntityListFormatter.php
index 2fd9b7c5507b4ce8e8713280c78d62dd26c9171e..3c94d96ee600789ce212296b1ba31bb49059f8cb 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace BookStack\Api;
 
+use BookStack\Entities\Models\BookChild;
 use BookStack\Entities\Models\Entity;
 use BookStack\Entities\Models\Page;
 
@@ -13,11 +14,6 @@ class ApiEntityListFormatter
      */
     protected array $list = [];
 
-    /**
-     * Whether to include related titles in the response.
-     */
-    protected bool $includeRelatedTitles = false;
-
     /**
      * The fields to show in the formatted data.
      * Can be a plain string array item for a direct model field (If existing on model).
@@ -77,22 +73,20 @@ class ApiEntityListFormatter
     }
 
     /**
-     * Enable the inclusion of related book and chapter titles in the response.
+     * Include parent book/chapter info in the formatted data.
      */
-    public function withRelatedTitles(): self
+    public function withParents(): self
     {
-        $this->includeRelatedTitles = true;
-
-        $this->withField('book_title', function (Entity $entity) {
-            if (method_exists($entity, 'book')) {
-                return $entity->book?->name;
+        $this->withField('book', function (Entity $entity) {
+            if ($entity instanceof BookChild && $entity->book) {
+                return $entity->book->only(['id', 'name', 'slug']);
             }
             return null;
         });
 
-        $this->withField('chapter_title', function (Entity $entity) {
-            if ($entity instanceof Page && $entity->chapter_id) {
-                return optional($entity->getAttribute('chapter'))->name;
+        $this->withField('chapter', function (Entity $entity) {
+            if ($entity instanceof Page && $entity->chapter) {
+                return $entity->chapter->only(['id', 'name', 'slug']);
             }
             return null;
         });
@@ -106,10 +100,6 @@ class ApiEntityListFormatter
      */
     public function format(): array
     {
-        if ($this->includeRelatedTitles) {
-            $this->loadRelatedTitles();
-        }
-
         $results = [];
 
         foreach ($this->list as $item) {
@@ -119,23 +109,6 @@ class ApiEntityListFormatter
         return $results;
     }
 
-    /**
-     * Eager load the related book and chapter data when needed.
-     */
-    protected function loadRelatedTitles(): void
-    {
-        $pages = collect($this->list)->filter(fn($item) => $item instanceof Page);
-
-        foreach ($this->list as $entity) {
-            if (method_exists($entity, 'book')) {
-                $entity->load('book');
-            }
-            if ($entity instanceof Page && $entity->chapter_id) {
-                $entity->load('chapter');
-            }
-        }
-    }
-
     /**
      * Format a single entity item to a plain array.
      */