Skip to content

Commit

Permalink
Simplify code after new default version id fallback in ::version
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jun 5, 2024
1 parent 4f7c56c commit 5199f6c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function query(
public function readContent(string|null $languageCode = null): array
{
try {
return $this->version(VersionId::default($this))->read($languageCode ?? 'default');
return $this->version()->read($languageCode ?? 'default');
} catch (NotFoundException) {
// only if the content file really does not exist, it's ok
// to return empty content. Otherwise this could lead to
Expand Down Expand Up @@ -745,14 +745,16 @@ public function version(VersionId|string|null $versionId = null): Version
public function writeContent(array $data, string|null $languageCode = null): bool
{
$data = $this->contentFileData($data, $languageCode);
$id = VersionId::default($this);

// update the default language, unless a specific language is passed
$languageCode ??= 'default';

try {
// we can only update if the version already exists
$this->version($id)->update($data, $languageCode ?? 'default');
$this->version()->update($data, $languageCode);
} catch (NotFoundException) {
// otherwise create a new version
$this->version($id)->create($data, $languageCode ?? 'default');
$this->version()->create($data, $languageCode);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ public function modified(
string|null $handler = null,
string|null $languageCode = null
): int|string|false|null {
$modified = $this->version(VersionId::default($this))->modified(
$modified = $this->version()->modified(
$languageCode ?? 'current'
);

Expand Down
2 changes: 1 addition & 1 deletion src/Content/ContentTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function content(): array
*/
public function contentFile(): string
{
return $this->contentFile = $this->parent->version(VersionId::default($this->parent))->contentFile($this->code);
return $this->contentFile = $this->parent->version()->contentFile($this->code);
}

/**
Expand Down

0 comments on commit 5199f6c

Please sign in to comment.