Skip to content

Commit

Permalink
add option to specify translatable fields in fields.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Mar 27, 2024
1 parent e23bb1d commit cc3aeef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 7 additions & 6 deletions classes/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ public function registerModelTranslation($widget)
return;
}


if (!$model->hasTranslatableAttributes()) {
return;
}

if ($widget->isNested && !empty($widget->fields)) {
if (($widget->config->translationMode ?? 'default') === 'fields') {
$widget->fields = $this->processFormMLFields($widget->fields, $model, $this->getWidgetLongName($widget));
Expand Down Expand Up @@ -202,14 +197,20 @@ protected function processFormMLFields($fields, $model, $parent = null)

foreach ($fields as $name => $config) {
$fieldName = $name;
$fieldTranslatable = false;

if (str_contains($name, '@')) {
// apply to fields with any context
list($fieldName, $context) = explode('@', $name);
}

$fieldName = $parent ? sprintf("%s[%s]", $parent, $fieldName) : $fieldName;

if (!array_key_exists($fieldName, $translatable)) {
if (array_get($config, 'translatable', false)) {
$model->addTranslatableAttributes($fieldName);
$fieldTranslatable = true;
}
if (!$fieldTranslatable && !array_key_exists($fieldName, $translatable)) {
continue;
}
$type = array_get($config, 'type', 'text');
Expand Down
4 changes: 4 additions & 0 deletions classes/TranslatableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function __construct($model)

$this->initTranslatableContext();

if (! isset($model->translatable)) {
$model->addDynamicProperty('translatable', []);
}

$this->model->bindEvent('model.beforeGetAttribute', function ($key) use ($model) {
if ($this->isTranslatable($key)) {
$value = $this->getAttributeTranslated($key);
Expand Down

0 comments on commit cc3aeef

Please sign in to comment.