From 9d4de5b634dcebb6a73e16814b62842138153520 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Thu, 20 Jun 2024 12:53:49 +0200 Subject: [PATCH 1/3] Simplify ::readContent and ::writeContent --- src/Cms/ModelWithContent.php | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Cms/ModelWithContent.php b/src/Cms/ModelWithContent.php index 7c9cb114ee..9962186f87 100644 --- a/src/Cms/ModelWithContent.php +++ b/src/Cms/ModelWithContent.php @@ -735,19 +735,7 @@ public function version(VersionId|string|null $versionId = null): Version */ public function writeContent(array $data, string|null $languageCode = null): bool { - $data = $this->contentFileData($data, $languageCode); - - // update the default language, unless a specific language is passed - $languageCode ??= 'default'; - - try { - // we can only update if the version already exists - $this->version()->update($data, $languageCode); - } catch (NotFoundException) { - // otherwise create a new version - $this->version()->create($data, $languageCode); - } - + $this->version()->save($data, $languageCode ?? 'current', true); return true; } } From cf01d12c4e7f6bddd50218fb46e0d05c299ce2a4 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Fri, 21 Jun 2024 13:21:20 +0200 Subject: [PATCH 2/3] New Language::is and Language::isCurrent --- src/Cms/Language.php | 21 ++++++++++++++ tests/Cms/Languages/LanguageTest.php | 43 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/Cms/Language.php b/src/Cms/Language.php index cc17d77340..4920d4196d 100644 --- a/src/Cms/Language.php +++ b/src/Cms/Language.php @@ -289,6 +289,27 @@ public function exists(): bool return file_exists($this->root()); } + /** + * Compare too language objects + */ + public function is(self $language): bool + { + return $this->id() === $language->id(); + } + + /** + * Checks if this is language is the + * current language + */ + public function isCurrent(): bool + { + if ($this->isSingle() === true) { + return true; + } + + return App::instance()->currentLanguage()->is($this); + } + /** * Checks if this is the default language * for the site. diff --git a/tests/Cms/Languages/LanguageTest.php b/tests/Cms/Languages/LanguageTest.php index 66864c78bb..1572c5e9e5 100644 --- a/tests/Cms/Languages/LanguageTest.php +++ b/tests/Cms/Languages/LanguageTest.php @@ -298,6 +298,49 @@ public function testExists() $this->assertTrue($language->exists()); } + /** + * @covers ::is + */ + public function testIs() + { + $en = new Language([ + 'code' => 'en' + ]); + + $de = new Language([ + 'code' => 'de' + ]); + + $this->assertTrue($en->is($en)); + $this->assertFalse($en->is($de)); + } + + /** + * @covers ::isCurrent + */ + public function testIsCurrent() + { + $this->app = $this->app->clone([ + 'languages' => [ + [ + 'code' => 'en', + 'default' => true + ], + [ + 'code' => 'de', + ] + ] + ]); + + $this->assertTrue($this->app->language('en')->isCurrent()); + $this->assertFalse($this->app->language('de')->isCurrent()); + + $this->app->setCurrentLanguage('de'); + + $this->assertTrue($this->app->language('de')->isCurrent()); + $this->assertFalse($this->app->language('en')->isCurrent()); + } + /** * @covers ::isDefault */ From 6127ef3d9cb5bb9dcafa7ba16afcfd6c46705c61 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Fri, 21 Jun 2024 14:26:39 +0200 Subject: [PATCH 3/3] Start to simplify ::content slightly --- src/Cms/ModelWithContent.php | 14 ++------------ tests/Cms/Models/ModelWithContentTest.php | 3 ++- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Cms/ModelWithContent.php b/src/Cms/ModelWithContent.php index 9962186f87..bf7e7e27e7 100644 --- a/src/Cms/ModelWithContent.php +++ b/src/Cms/ModelWithContent.php @@ -132,21 +132,11 @@ public function content(string|null $languageCode = null): Content { // single language support if ($this->kirby()->multilang() === false) { - if ($this->content instanceof Content) { - return $this->content; - } - - // don't normalize field keys (already handled by the `Data` class) - return $this->content = new Content($this->readContent(), $this, false); + return $this->content ??= $this->version()->content('default'); } // get the targeted language - $language = $this->kirby()->language($languageCode); - - // stop if the language does not exist - if ($language === null) { - throw new InvalidArgumentException('Invalid language: ' . $languageCode); - } + $language = Language::ensure($languageCode); // only fetch from cache for the current language if ($languageCode === null && $this->content instanceof Content) { diff --git a/tests/Cms/Models/ModelWithContentTest.php b/tests/Cms/Models/ModelWithContentTest.php index b237f2d649..0ad69435e8 100644 --- a/tests/Cms/Models/ModelWithContentTest.php +++ b/tests/Cms/Models/ModelWithContentTest.php @@ -6,6 +6,7 @@ use Kirby\Content\Version; use Kirby\Content\VersionId; use Kirby\Exception\InvalidArgumentException; +use Kirby\Exception\NotFoundException; use Kirby\Panel\Page as PanelPage; use Kirby\Uuid\PageUuid; use Kirby\Uuid\SiteUuid; @@ -155,7 +156,7 @@ public function testContentForInvalidTranslation() ] ]); - $this->expectException(InvalidArgumentException::class); + $this->expectException(NotFoundException::class); $this->expectExceptionMessage('Invalid language: fr'); $app->page('foo')->content('fr');