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

Persist non-translatable field with correct default locale #1384

Merged
merged 5 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Configuration/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/Configuration/Content/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ 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
Expand Down
11 changes: 10 additions & 1 deletion src/Configuration/Parser/ContentTypesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ public function addField(Field $field): self

$this->fields[] = $field;
$field->setContent($this);
$field->setDefaultLocaleFromContent();

return $this;
}
Expand Down
19 changes: 0 additions & 19 deletions src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/Event/Listener/ContentFillListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -93,11 +92,4 @@ private function guesstimateAuthor($contenttype): User

return $user;
}

private function setFieldsDefaultLocales(Content $entity): void
{
foreach ($entity->getRawFields() as $field) {
$field->setDefaultLocaleFromContent();
}
}
}
12 changes: 7 additions & 5 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
$container->setParameter('container.dumper.inline_factories', true);
$confDir = $this->getProjectDir() . '/config';

$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) {
Expand Down Expand Up @@ -123,7 +123,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('|');
Expand Down
3 changes: 3 additions & 0 deletions src/Repository/FieldRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/php/Configuration/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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'));
}
Expand All @@ -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'));
Expand All @@ -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();
Expand Down
20 changes: 12 additions & 8 deletions tests/php/Configuration/Parser/ContentTypesParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -87,14 +89,16 @@ 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
{
$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();
Expand All @@ -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();

Expand Down