Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Feb 20, 2024
1 parent 2e30b7e commit 0ccb6c4
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions formwidgets/MLRepeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ class MLRepeater extends Repeater
*/
protected $defaultAlias = 'mlrepeater';

protected $translateFields = false;
/**
* The repeater translation mode (repeater|fields)
*/
protected $translationMode = 'repeater';

/**
* {@inheritDoc}
*/
public function init()
{
parent::init();

$this->fillFromConfig(['translateFields']);
$this->initLocale();

if ($this->translateFields && $this->model) {
$this->fillFromConfig(['translationMode']);

if ($this->translationMode === 'fields' && $this->model) {
$this->model->extend(function () {
$this->addDynamicMethod('getJsonAttributeTranslated', function ($key, $locale) {
$names = HtmlHelper::nameToArray($key);
Expand Down Expand Up @@ -64,7 +67,7 @@ public function render()
$parentContent = parent::render();
$this->actAsParent(false);

if (!$this->isAvailable || $this->translateFields) {
if (!$this->isAvailable || $this->translationMode === 'fields') {
return $parentContent;
}

Expand All @@ -84,36 +87,31 @@ public function prepareVars()
*/
public function getSaveValue($value)
{
if (!$this->translateFields) {
$this->rewritePostValues();
}

return $this->getLocaleSaveValue(is_array($value) ? array_values($value) : $value);
}

public function getLocaleSaveValue($value)
{
if (!$this->translateFields) {
return parent::getLocaleSaveValue(is_array($value) ? array_values($value) : $value);
}

$fieldName = implode('.', HtmlHelper::nameToArray($this->formField->getName()));

foreach (post('RLTranslate') as $locale => $_data) {
$items = array_get($_data, $fieldName, []);
foreach ($items as $index => $item) {
foreach ($item as $field => $fieldValue) {
if ($locale === $this->defaultLocale->code) {
$value[$index][$field] = $fieldValue;
} else {
$key = sprintf("locale%s", ucfirst($field));
$value[$index][$key][$locale] = $fieldValue;
$value = is_array($value) ? array_values($value) : $value;

// process internal fields translations
if ($this->translationMode === 'fields') {
$fieldName = implode('.', HtmlHelper::nameToArray($this->formField->getName()));

foreach (post('RLTranslate') as $locale => $_data) {
$items = array_get($_data, $fieldName, []);
foreach ($items as $index => $item) {
foreach ($item as $field => $fieldValue) {
if ($locale === $this->defaultLocale->code) {
$value[$index][$field] = $fieldValue;
} else {
$key = sprintf("locale%s", ucfirst($field));
$value[$index][$key][$locale] = $fieldValue;
}
}
}
}
return $value;
}

return $value;
// we translate the repeater formwidget itself ($this->translationMode === 'repeater')
$this->rewritePostValues();
return $this->getLocaleSaveValue($value);
}

/**
Expand All @@ -125,7 +123,7 @@ protected function loadAssets()
parent::loadAssets();
$this->actAsParent(false);

if (Locale::isAvailable() && !$this->translateFields) {
if (Locale::isAvailable() && $this->translationMode === 'repeater') {
$this->loadLocaleAssets();
$this->addJs('js/mlrepeater.js');
}
Expand Down

0 comments on commit 0ccb6c4

Please sign in to comment.