Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes 21: Render Mode #6593

Merged
merged 8 commits into from
Aug 13, 2024
19 changes: 15 additions & 4 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved

if ($version->exists($language) === true) {
return $version->content($language);
}
distantnative marked this conversation as resolved.
Show resolved Hide resolved
}

// 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;
Expand Down
18 changes: 17 additions & 1 deletion src/Content/VersionId.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
14 changes: 13 additions & 1 deletion src/Panel/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
return Form::for(
model: $this->model,
props: [
'values' => $changes
]
)->values();
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Panel/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,11 @@ public function props(): array
*/
public function view(): array
{
$page = $this->model;

return [
'breadcrumb' => $page->panel()->breadcrumb(),
'breadcrumb' => $this->model->panel()->breadcrumb(),
'component' => 'k-page-view',
'props' => $this->props(),
'title' => $page->title()->toString(),
'title' => $this->model->title()->toString(),
];
}
}
33 changes: 33 additions & 0 deletions tests/Cms/Models/ModelWithContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading