From 80becd7033299d2810cd8624ed582f3d3456377d Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Wed, 31 Jul 2024 16:43:18 +0200 Subject: [PATCH 1/8] New $render property to register the rendered content version --- src/Content/VersionId.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Content/VersionId.php b/src/Content/VersionId.php index 62b2c5be47..e1dc0c26c8 100644 --- a/src/Content/VersionId.php +++ b/src/Content/VersionId.php @@ -32,7 +32,13 @@ class VersionId implements Stringable /** * Latest changes to the content (optional) */ - public const CHANGES = 'changes'; + public const CHANGES = 'changes'; + + /** + * A global store for a version id that should be + * rendered for each model in a live preview scenario. + */ + public static self|null $render = null; /** * @throws \Kirby\Exception\InvalidArgumentException If the version ID is not valid @@ -104,6 +110,16 @@ public static function published(): static return new static(static::PUBLISHED); } + /** + * Returns the VersionId which should be rendered for the given + * Model. This can be changed by overwriting the static `::$render` property + * and thus can be used to render different versions for all models (e.g. in a preview) + */ + public static function render(ModelWithContent $model): static + { + return static::$render ?? static::default($model); + } + /** * Returns the ID value */ From 44feac1e991718de14e28325c24f5a2cc1ef827e Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Wed, 31 Jul 2024 16:43:46 +0200 Subject: [PATCH 2/8] Use the new render mode in models and activate it in the panel --- src/Cms/ModelWithContent.php | 19 +++++++++++++++---- src/Panel/Page.php | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Cms/ModelWithContent.php b/src/Cms/ModelWithContent.php index 090d782d38..cf0b20ce52 100644 --- a/src/Cms/ModelWithContent.php +++ b/src/Cms/ModelWithContent.php @@ -129,14 +129,25 @@ abstract protected function commit( */ public function content(string|null $languageCode = null): Content { + // get the targeted language + $language = Language::ensure($languageCode); + + // fetch a specific version in preview render mode + // @todo this entire block can be radically simplified as soon + // as the models use the versions exclusively. + if (VersionId::$render ?? null) { + $version = $this->version(VersionId::render($this)); + + if ($version->exists($language) === true) { + return $version->content($language); + } + } + // single language support if ($this->kirby()->multilang() === false) { - return $this->content ??= $this->version()->content('default'); + return $this->content ??= $this->version()->content($language); } - // get the targeted language - $language = Language::ensure($languageCode); - // only fetch from cache for the current language if ($languageCode === null && $this->content instanceof Content) { return $this->content; diff --git a/src/Panel/Page.php b/src/Panel/Page.php index 07dd87a497..59b8926ad0 100644 --- a/src/Panel/Page.php +++ b/src/Panel/Page.php @@ -4,6 +4,7 @@ use Kirby\Cms\File as CmsFile; use Kirby\Cms\ModelWithContent; +use Kirby\Content\VersionId; use Kirby\Filesystem\Asset; use Kirby\Panel\Ui\Buttons\ViewButtons; use Kirby\Toolkit\I18n; @@ -373,11 +374,23 @@ public function view(): array { $page = $this->model; - return [ + // This is placed here as a proof of concept + // @todo we need to find a better place for it + // to switch into changes mode in the panel + VersionId::$render = VersionId::changes(); + + $view = [ 'breadcrumb' => $page->panel()->breadcrumb(), 'component' => 'k-page-view', 'props' => $this->props(), 'title' => $page->title()->toString(), ]; + + // The render mode needs to be undone here to keep unit tests working + // That's the downside of such static properties. They are likely to + // cause side-effects + VersionId::$render = null; + + return $view; } } From 200e6c2490932a5f22a6a58a0603d1c7590d09be Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 12 Aug 2024 11:38:03 +0200 Subject: [PATCH 3/8] Revert using the VersionId render mode in the Panel --- src/Panel/Model.php | 14 +++++++++++++- src/Panel/Page.php | 21 +++------------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Panel/Model.php b/src/Panel/Model.php index 707e2f1a5f..482fb222b1 100644 --- a/src/Panel/Model.php +++ b/src/Panel/Model.php @@ -37,7 +37,19 @@ abstract public function buttons(): array; */ public function content(): array { - return Form::for($this->model)->values(); + $version = $this->model->version('changes'); + $changes = []; + + if ($version->exists() === true) { + $changes = $version->content()->toArray(); + } + + return Form::for( + model: $this->model, + props: [ + 'values' => $changes + ] + )->values(); } /** diff --git a/src/Panel/Page.php b/src/Panel/Page.php index 59b8926ad0..1f92d007ef 100644 --- a/src/Panel/Page.php +++ b/src/Panel/Page.php @@ -4,7 +4,6 @@ use Kirby\Cms\File as CmsFile; use Kirby\Cms\ModelWithContent; -use Kirby\Content\VersionId; use Kirby\Filesystem\Asset; use Kirby\Panel\Ui\Buttons\ViewButtons; use Kirby\Toolkit\I18n; @@ -372,25 +371,11 @@ public function props(): array */ public function view(): array { - $page = $this->model; - - // This is placed here as a proof of concept - // @todo we need to find a better place for it - // to switch into changes mode in the panel - VersionId::$render = VersionId::changes(); - - $view = [ - 'breadcrumb' => $page->panel()->breadcrumb(), + return [ + 'breadcrumb' => $this->model->panel()->breadcrumb(), 'component' => 'k-page-view', 'props' => $this->props(), - 'title' => $page->title()->toString(), + 'title' => $this->model->title()->toString(), ]; - - // The render mode needs to be undone here to keep unit tests working - // That's the downside of such static properties. They are likely to - // cause side-effects - VersionId::$render = null; - - return $view; } } From e925439d0e49eeaad3a998bbc5a0890f7dd3579a Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 12 Aug 2024 11:51:23 +0200 Subject: [PATCH 4/8] Add unit tests for content method with different render modes --- tests/Cms/Models/ModelWithContentTest.php | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Cms/Models/ModelWithContentTest.php b/tests/Cms/Models/ModelWithContentTest.php index 8540e4aa04..52dd2668cf 100644 --- a/tests/Cms/Models/ModelWithContentTest.php +++ b/tests/Cms/Models/ModelWithContentTest.php @@ -230,6 +230,39 @@ public function testContentLockWithNoDirectory() $this->assertNull($model->lock()); } + public function testContentWithChanges() + { + $app = new App([ + 'roots' => [ + 'index' => static::TMP + ], + 'site' => [ + 'children' => [ + [ + 'slug' => 'foo', + ] + ], + ] + ]); + + $page = $app->page('foo'); + + $this->assertSame(null, $page->content()->title()->value()); + + // create some changes + $page->version('changes')->save([ + 'title' => 'Test' + ]); + + VersionId::$render = VersionId::changes(); + + $this->assertSame('Test', $page->content()->title()->value()); + + VersionId::$render = null; + + $this->assertSame(null, $page->content()->title()->value()); + } + /** * @dataProvider modelsProvider */ From 3a4f523b37cb591b9890afb351eeaab420e9f6b2 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 13 Aug 2024 11:09:45 +0200 Subject: [PATCH 5/8] Simplify code to get the version id --- src/Cms/ModelWithContent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cms/ModelWithContent.php b/src/Cms/ModelWithContent.php index cf0b20ce52..e945b81e2d 100644 --- a/src/Cms/ModelWithContent.php +++ b/src/Cms/ModelWithContent.php @@ -136,7 +136,7 @@ public function content(string|null $languageCode = null): Content // @todo this entire block can be radically simplified as soon // as the models use the versions exclusively. if (VersionId::$render ?? null) { - $version = $this->version(VersionId::render($this)); + $version = $this->version(VersionId::$render); if ($version->exists($language) === true) { return $version->content($language); From 61c8061b69611c29cdbd6fb28f59b37ee0b35bca Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 13 Aug 2024 11:10:21 +0200 Subject: [PATCH 6/8] Improve inline documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nico Hoffmann เทด. --- src/Panel/Model.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Panel/Model.php b/src/Panel/Model.php index 482fb222b1..6519b79429 100644 --- a/src/Panel/Model.php +++ b/src/Panel/Model.php @@ -43,7 +43,8 @@ public function content(): array if ($version->exists() === true) { $changes = $version->content()->toArray(); } - +// create a form which will collect the published values for the model, +// but also pass along unpublished changes as overwrites return Form::for( model: $this->model, props: [ From bbd54b138a431f943c151e64e101bbc1dd6b88aa Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 13 Aug 2024 11:15:09 +0200 Subject: [PATCH 7/8] Fix indentation --- src/Panel/Model.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Panel/Model.php b/src/Panel/Model.php index 6519b79429..dc6798cc35 100644 --- a/src/Panel/Model.php +++ b/src/Panel/Model.php @@ -43,8 +43,9 @@ public function content(): array if ($version->exists() === true) { $changes = $version->content()->toArray(); } -// create a form which will collect the published values for the model, -// but also pass along unpublished changes as overwrites + + // create a form which will collect the published values for the model, + // but also pass along unpublished changes as overwrites return Form::for( model: $this->model, props: [ From 1d57f1871a8e756ebf533861b76b1de34184b7ee Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Tue, 13 Aug 2024 11:34:43 +0200 Subject: [PATCH 8/8] Remove unnecessary render method --- src/Content/VersionId.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Content/VersionId.php b/src/Content/VersionId.php index e1dc0c26c8..58a06aaee9 100644 --- a/src/Content/VersionId.php +++ b/src/Content/VersionId.php @@ -110,16 +110,6 @@ public static function published(): static return new static(static::PUBLISHED); } - /** - * Returns the VersionId which should be rendered for the given - * Model. This can be changed by overwriting the static `::$render` property - * and thus can be used to render different versions for all models (e.g. in a preview) - */ - public static function render(ModelWithContent $model): static - { - return static::$render ?? static::default($model); - } - /** * Returns the ID value */