From 66c6fe826676fe4f9a8dc5e9df7551340d8c8ffe Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 18 May 2020 14:14:56 +0200 Subject: [PATCH 1/5] Fields persisted with correct default locale --- src/Configuration/Config.php | 9 +++++++-- .../Parser/ContentTypesParser.php | 11 +++++++++- src/Entity/Field.php | 19 ------------------ src/Event/Listener/ContentFillListener.php | 8 -------- src/Kernel.php | 10 +++++++--- src/Repository/FieldRepository.php | 3 +++ tests/php/Configuration/ConfigTest.php | 8 ++++---- .../Parser/ContentTypesParserTest.php | 20 +++++++++++-------- 8 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/Configuration/Config.php b/src/Configuration/Config.php index 96aa8ec67..fa3a3aa11 100644 --- a/src/Configuration/Config.php +++ b/src/Configuration/Config.php @@ -36,12 +36,17 @@ class Config /** @var string */ private $locales; - public function __construct(string $locales, Stopwatch $stopwatch, string $projectDir, CacheInterface $cache, string $publicFolder) + /** @var string */ + private $defaultLocale; + + public function __construct(string $locales, string $defaultLocale, Stopwatch $stopwatch, string $projectDir, CacheInterface $cache, string $publicFolder) { $this->locales = $locales; $this->stopwatch = $stopwatch; $this->cache = $cache; $this->projectDir = $projectDir; + $this->defaultLocale = $defaultLocale; + $this->data = $this->getConfig(); // @todo PathResolver shouldn't be part of Config. Refactor to separate class @@ -88,7 +93,7 @@ private function parseConfig(): array $taxonomy = new TaxonomyParser($this->projectDir); $config['taxonomies'] = $taxonomy->parse(); - $contentTypes = new ContentTypesParser($this->locales, $this->projectDir, $config->get('general')); + $contentTypes = new ContentTypesParser($this->projectDir, $config->get('general'), $this->defaultLocale, $this->locales); $config['contenttypes'] = $contentTypes->parse(); $menu = new MenuParser($this->projectDir); diff --git a/src/Configuration/Parser/ContentTypesParser.php b/src/Configuration/Parser/ContentTypesParser.php index b1053ffe0..45990b47a 100644 --- a/src/Configuration/Parser/ContentTypesParser.php +++ b/src/Configuration/Parser/ContentTypesParser.php @@ -20,10 +20,14 @@ class ContentTypesParser extends BaseParser /** @var array */ private $localeCodes = []; - public function __construct(?string $locales = null, string $projectDir, Collection $generalConfig, string $filename = 'contenttypes.yaml') + /** @var string defaultLocale */ + private $defaultLocale; + + public function __construct(string $projectDir, Collection $generalConfig, string $defaultLocale, ?string $locales = null, string $filename = 'contenttypes.yaml') { $this->localeCodes = empty($locales) ? [] : explode('|', $locales); $this->generalConfig = $generalConfig; + $this->defaultLocale = $defaultLocale; parent::__construct($projectDir, $filename); } @@ -306,6 +310,11 @@ private function parseField($key, &$field, $acceptFileTypes, &$currentGroup): vo } else { $currentGroup = $field['group']; } + + // Initialise default_locale, if not explicit + if (! isset($field['default_locale'])) { + $field['default_locale'] = $this->defaultLocale; + } } /** diff --git a/src/Entity/Field.php b/src/Entity/Field.php index e1754c08b..92701fcda 100644 --- a/src/Entity/Field.php +++ b/src/Entity/Field.php @@ -308,25 +308,6 @@ public function getLocale(): ?string return $this->getCurrentLocale(); } - public function setDefaultLocaleFromContent(): self - { - if ($this->getContent() === null) { - return $this; - } - - $locales = $this->getContent()->getDefinition()->get('locales'); - - if (empty($locales) || $locales->isEmpty()) { - return $this; - } - - $defaultLocale = $locales->first(); - - $this->setDefaultLocale($defaultLocale); - - return $this; - } - public function getVersion(): ?int { return $this->version; diff --git a/src/Event/Listener/ContentFillListener.php b/src/Event/Listener/ContentFillListener.php index dc3f8aa11..a6300b980 100644 --- a/src/Event/Listener/ContentFillListener.php +++ b/src/Event/Listener/ContentFillListener.php @@ -80,7 +80,6 @@ public function fillContent(Content $entity): void { $entity->setDefinitionFromContentTypesConfig($this->config->get('contenttypes')); $entity->setContentExtension($this->contentExtension); - $this->setFieldsDefaultLocales($entity); } private function guesstimateAuthor($contenttype): User @@ -93,11 +92,4 @@ private function guesstimateAuthor($contenttype): User return $user; } - - private function setFieldsDefaultLocales(Content $entity): void - { - foreach ($entity->getRawFields() as $field) { - $field->setDefaultLocaleFromContent(); - } - } } diff --git a/src/Kernel.php b/src/Kernel.php index 25ab0f57d..aed17c661 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -58,13 +58,15 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $this->setBoltParameters($container, $confDir); $this->setContentTypeRequirements($container); - $this->setTaxonomyRequirements($container); - $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob'); $loader->load($confDir . '/{packages}/' . $this->environment . '/*' . self::CONFIG_EXTS, 'glob'); $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob'); $loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob'); + $this->setBoltParameters($container, $confDir); + $this->setContentTypeRequirements($container); + $this->setTaxonomyRequirements($container); + try { $loader->load($confDir . '/{services}_bolt' . self::CONFIG_EXTS, 'glob'); } catch (\Throwable $e) { @@ -123,7 +125,9 @@ private function flattenKeys(array $array, string $prefix = ''): array */ private function setContentTypeRequirements(ContainerBuilder $container): void { - $ContentTypesParser = new ContentTypesParser(null, $this->getProjectDir(), new Collection()); + /** @var string $defaultLocale */ + $defaultLocale = $container->getParameter('locale'); + $ContentTypesParser = new ContentTypesParser($this->getProjectDir(), new Collection(), $defaultLocale); $contentTypes = $ContentTypesParser->parse(); $pluralslugs = $contentTypes->pluck('slug')->implode('|'); diff --git a/src/Repository/FieldRepository.php b/src/Repository/FieldRepository.php index 594af134c..8723c656d 100644 --- a/src/Repository/FieldRepository.php +++ b/src/Repository/FieldRepository.php @@ -85,6 +85,9 @@ public static function factory(Collection $definition, string $name = '', string $field->setLabel($label); } + $field->setDefaultLocale($definition['default_locale']); + $field->setLocale($definition['default_locale']); + return $field; } } diff --git a/tests/php/Configuration/ConfigTest.php b/tests/php/Configuration/ConfigTest.php index 04515e9c0..3b4a5fca7 100644 --- a/tests/php/Configuration/ConfigTest.php +++ b/tests/php/Configuration/ConfigTest.php @@ -17,7 +17,7 @@ public function testCanParse(): void { $projectDir = dirname(dirname(dirname(__DIR__))); $cache = new TraceableAdapter(new FilesystemAdapter()); - $config = new Config('', new Stopwatch(), $projectDir, $cache, 'public'); + $config = new Config('', '', new Stopwatch(), $projectDir, $cache, 'public'); $this->assertInstanceOf(Config::class, $config); } @@ -26,7 +26,7 @@ public function testConfigGet(): void { $projectDir = dirname(dirname(dirname(__DIR__))); $cache = new TraceableAdapter(new FilesystemAdapter()); - $config = new Config('', new Stopwatch(), $projectDir, $cache, 'public'); + $config = new Config('', '', new Stopwatch(), $projectDir, $cache, 'public'); $this->assertSame('Bolt Core Git Clone', $config->get('general/sitename')); } @@ -35,7 +35,7 @@ public function testConfigHas(): void { $projectDir = dirname(dirname(dirname(__DIR__))); $cache = new TraceableAdapter(new FilesystemAdapter()); - $config = new Config('', new Stopwatch(), $projectDir, $cache, 'public'); + $config = new Config('', '', new Stopwatch(), $projectDir, $cache, 'public'); $this->assertTrue($config->has('general/payoff')); $this->assertFalse($config->has('general/payoffXXXXX')); @@ -45,7 +45,7 @@ public function testConfigGetMediaTypes(): void { $projectDir = dirname(dirname(dirname(__DIR__))); $cache = new TraceableAdapter(new FilesystemAdapter()); - $config = new Config('', new Stopwatch(), $projectDir, $cache, 'public'); + $config = new Config('', '', new Stopwatch(), $projectDir, $cache, 'public'); /** @var Collection $mediaTypes */ $mediaTypes = $config->getMediaTypes(); diff --git a/tests/php/Configuration/Parser/ContentTypesParserTest.php b/tests/php/Configuration/Parser/ContentTypesParserTest.php index a7b592fd8..7918122d5 100644 --- a/tests/php/Configuration/Parser/ContentTypesParserTest.php +++ b/tests/php/Configuration/Parser/ContentTypesParserTest.php @@ -17,14 +17,16 @@ class ContentTypesParserTest extends ParserTestBase public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 24; - public const AMOUNT_OF_ATTRIBUTES_IN_FIELD = 23; + public const AMOUNT_OF_ATTRIBUTES_IN_FIELD = 24; public const ALLOWED_LOCALES = 'en|nl|es|fr|de|pl|it|hu|pt_BR|ja|nb|nn|nl_NL|nl_BE'; + public const DEFAULT_LOCALE = 'nl'; + public function testCanParse(): void { $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse()); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES); $config = $contentTypesParser->parse(); $this->assertInstanceOf(Collection::class, $config); @@ -34,7 +36,7 @@ public function testIgnoreNonsensicalFileParse(): void { $file = self::getBasePath() . 'bogus.yaml'; $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse(), $file); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES, $file); $this->expectException(ConfigurationException::class); @@ -45,7 +47,7 @@ public function testBreakOnInvalidFileParse(): void { $file = self::getBasePath() . 'broken.yaml'; $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse(), $file); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES, $file); $this->expectException(ParseException::class); @@ -55,7 +57,7 @@ public function testBreakOnInvalidFileParse(): void public function testBreakOnMissingFileParse(): void { $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse(), 'foo.yml'); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES, 'foo.yml'); $this->expectException(FileLocatorFileNotFoundException::class); @@ -65,7 +67,7 @@ public function testBreakOnMissingFileParse(): void public function testHasConfig(): void { $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse()); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES); $config = $contentTypesParser->parse(); $this->assertCount(6, $config); @@ -87,6 +89,8 @@ public function testHasConfig(): void $this->assertSame('fa-home', $config['homepage']['icon_one']); $this->assertFalse($config['homepage']['allow_numeric_slugs']); $this->assertContains('nl', $config['homepage']['locales']); + $this->assertContains('ja', $config['homepage']['locales']); + $this->assertSame('nl', $config['homepage']['fields']['title']['default_locale']); } public function testBrokenContentTypeValues(): void @@ -94,7 +98,7 @@ public function testBrokenContentTypeValues(): void $file = self::getBasePath() . 'broken_content_types.yaml'; $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse(), $file); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES, $file); $this->expectException(ConfigurationException::class); $contentTypesParser->parse(); @@ -105,7 +109,7 @@ public function testInferContentTypeValues(): void $file = self::getBasePath() . 'minimal_content_types.yaml'; $generalParser = new GeneralParser($this->getProjectDir()); - $contentTypesParser = new ContentTypesParser(self::ALLOWED_LOCALES, $this->getProjectDir(), $generalParser->parse(), $file); + $contentTypesParser = new ContentTypesParser($this->getProjectDir(), $generalParser->parse(), self::DEFAULT_LOCALE, self::ALLOWED_LOCALES, $file); $config = $contentTypesParser->parse(); From bd9e5430ddedf18869fd5311f36161a583ae2ee9 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 18 May 2020 14:16:06 +0200 Subject: [PATCH 2/5] Update Kernel.php --- src/Kernel.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Kernel.php b/src/Kernel.php index aed17c661..9b563962b 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -56,8 +56,6 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $container->setParameter('container.dumper.inline_factories', true); $confDir = $this->getProjectDir() . '/config'; - $this->setBoltParameters($container, $confDir); - $this->setContentTypeRequirements($container); $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob'); $loader->load($confDir . '/{packages}/' . $this->environment . '/*' . self::CONFIG_EXTS, 'glob'); $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob'); From 094f62fdbb3d9e9b4985e9b89aaf06d3127c7d1a Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 18 May 2020 14:17:57 +0200 Subject: [PATCH 3/5] csfix. default_locale in FieldType --- src/Configuration/Config.php | 2 +- src/Configuration/Content/FieldType.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Configuration/Config.php b/src/Configuration/Config.php index fa3a3aa11..ecb73e139 100644 --- a/src/Configuration/Config.php +++ b/src/Configuration/Config.php @@ -93,7 +93,7 @@ private function parseConfig(): array $taxonomy = new TaxonomyParser($this->projectDir); $config['taxonomies'] = $taxonomy->parse(); - $contentTypes = new ContentTypesParser($this->projectDir, $config->get('general'), $this->defaultLocale, $this->locales); + $contentTypes = new ContentTypesParser($this->projectDir, $config->get('general'), $this->defaultLocale, $this->locales); $config['contenttypes'] = $contentTypes->parse(); $menu = new MenuParser($this->projectDir); diff --git a/src/Configuration/Content/FieldType.php b/src/Configuration/Content/FieldType.php index 89ed9f16b..dd3104c70 100644 --- a/src/Configuration/Content/FieldType.php +++ b/src/Configuration/Content/FieldType.php @@ -48,12 +48,15 @@ private static function defaults(): Collection 'error' => false, 'pattern' => false, 'hidden' => false, + 'default_locale' => 'en', ]); } public static function factory(string $name, ContentType $contentType): self { - return new self($contentType->get('fields')->get($name, [])); + $defaults = static::defaults(); + $fromContentType = new self($contentType->get('fields')->get($name, [])); + return new self($defaults->merge($fromContentType)); } public static function mock(string $name, ?Collection $definition = null): self From c4d14fcec2f9e55479f9149ac831f4de7b423eca Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 18 May 2020 15:37:54 +0200 Subject: [PATCH 4/5] Clean removed method --- src/Entity/Content.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Entity/Content.php b/src/Entity/Content.php index 2027be434..95a172651 100644 --- a/src/Entity/Content.php +++ b/src/Entity/Content.php @@ -520,7 +520,6 @@ public function addField(Field $field): self $this->fields[] = $field; $field->setContent($this); - $field->setDefaultLocaleFromContent(); return $this; } From 8f06fc17a100d00be9ce14a3547281a4027b9543 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 18 May 2020 15:51:11 +0200 Subject: [PATCH 5/5] csfix --- src/Configuration/Content/FieldType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Configuration/Content/FieldType.php b/src/Configuration/Content/FieldType.php index dd3104c70..33e5ee012 100644 --- a/src/Configuration/Content/FieldType.php +++ b/src/Configuration/Content/FieldType.php @@ -56,6 +56,7 @@ public static function factory(string $name, ContentType $contentType): self { $defaults = static::defaults(); $fromContentType = new self($contentType->get('fields')->get($name, [])); + return new self($defaults->merge($fromContentType)); }