Skip to content

Commit

Permalink
Merge pull request #1300 from bolt/bugfix/locale-fallback-value
Browse files Browse the repository at this point in the history
Field::getValue() returns defaultLocale value (if current locale value is empty empty) for non-localizable fields
  • Loading branch information
bobdenotter authored Apr 16, 2020
2 parents 8165fcb + d7e58fb commit bf2daca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,19 @@ public function getApiValue()

public function getValue(): ?array
{
return $this->translate($this->getCurrentLocale(), ! $this->isTranslatable())->getValue();
$value = $this->translate($this->getCurrentLocale(), false)->getValue();

// If the field is not translatable, return the value without fallback to defaultLocale
if ($this->isTranslatable()) {
return $value;
}

// If value is empty, get the defaultLocale as fallback.
if (empty($value)) {
$value = $this->translate($this->getDefaultLocale(), false)->getValue();
}

return $value;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ public function getTitle(Content $content, string $locale = ''): string
$field->setCurrentLocale($locale);
}

$titleParts[] = $content->getField($fieldName)->__toString();
$value = $field->getParsedValue();

if (empty($value)) {
$value = $field->setLocale($field->getDefaultLocale())->getParsedValue();
}

$titleParts[] = $value;
}

$maxLength = 80; // Should we make this configurable, or is that overkill?
Expand Down

0 comments on commit bf2daca

Please sign in to comment.