X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/6e19a8a4bb8c37c641d82e506ed53598495d950b..c1d76d2571fcd125064d245796e909240c79d86b:/app/Api/ApiEntityListFormatter.php diff --git a/app/Api/ApiEntityListFormatter.php b/app/Api/ApiEntityListFormatter.php index 436d66d59..3c94d96ee 100644 --- a/app/Api/ApiEntityListFormatter.php +++ b/app/Api/ApiEntityListFormatter.php @@ -2,7 +2,9 @@ namespace BookStack\Api; +use BookStack\Entities\Models\BookChild; use BookStack\Entities\Models\Entity; +use BookStack\Entities\Models\Page; class ApiEntityListFormatter { @@ -20,8 +22,16 @@ class ApiEntityListFormatter * @var array */ protected array $fields = [ - 'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft', - 'template', 'priority', 'created_at', 'updated_at', + 'id', + 'name', + 'slug', + 'book_id', + 'chapter_id', + 'draft', + 'template', + 'priority', + 'created_at', + 'updated_at', ]; public function __construct(array $list) @@ -62,6 +72,28 @@ class ApiEntityListFormatter return $this; } + /** + * Include parent book/chapter info in the formatted data. + */ + public function withParents(): self + { + $this->withField('book', function (Entity $entity) { + if ($entity instanceof BookChild && $entity->book) { + return $entity->book->only(['id', 'name', 'slug']); + } + return null; + }); + + $this->withField('chapter', function (Entity $entity) { + if ($entity instanceof Page && $entity->chapter) { + return $entity->chapter->only(['id', 'name', 'slug']); + } + return null; + }); + + return $this; + } + /** * Format the data and return an array of formatted content. * @return array[]