Skip to content

Commit ee64c01

Browse files
author
Phillip Dornauer
committed
Fix translation fallback: apply only on frontend, not in admin editor
- Add translationFallbackEnabled() flag to MarbleManager - Enable fallback in Marble::routes() handler (frontend only) - Non-translatable fields still always fall back to primary language - Translatable fields only fall back when frontend flag is active - Fixes admin editor showing EN values in DE/SK fields
1 parent aea57cb commit ee64c01

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/MarbleManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class MarbleManager
1717
protected FieldTypeRegistry $registry;
1818
protected ?int $currentLanguageId = null;
1919
protected ?int $primaryLanguageId = null;
20+
protected bool $translationFallback = false;
2021

2122
public function __construct(FieldTypeRegistry $registry)
2223
{
@@ -102,6 +103,16 @@ public function primaryLanguageId(): int
102103
return $this->primaryLanguageId;
103104
}
104105

106+
public function enableTranslationFallback(): void
107+
{
108+
$this->translationFallback = true;
109+
}
110+
111+
public function translationFallbackEnabled(): bool
112+
{
113+
return $this->translationFallback;
114+
}
115+
105116
// -------------------------------------------------------------------------
106117
// Item API (convenience, cached)
107118
// -------------------------------------------------------------------------
@@ -185,6 +196,7 @@ public function routes(callable $handler, string $prefix = ''): void
185196
$pattern = $pattern ? $pattern . '/{path?}' : '{path?}';
186197

187198
Route::get($pattern, function (string $path = '') use ($handler, $prefix) {
199+
$this->enableTranslationFallback();
188200
$fullPath = ($prefix ? rtrim($prefix, '/') : '') . '/' . ltrim($path, '/');
189201
$item = $this->resolveOrFail($fullPath);
190202
return $handler($item);

src/Models/Item.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ protected function loadValuesForLanguage(int $languageId): void
456456
->get()
457457
->keyBy('blueprint_field_id');
458458

459-
// For non-translatable fields, also load the primary language values
460459
$primaryLanguageId = Marble::primaryLanguageId();
461460

462461
if ($languageId !== $primaryLanguageId) {
@@ -465,15 +464,19 @@ protected function loadValuesForLanguage(int $languageId): void
465464
->get()
466465
->keyBy('blueprint_field_id');
467466

468-
// Fill in from primary language:
469-
// - non-translatable fields: always
470-
// - translatable fields: when no row exists, or the row is empty
471467
foreach ($this->blueprint->fields as $field) {
472468
if (!isset($primaryValues[$field->id])) {
473469
continue;
474470
}
475471
$existing = $values[$field->id] ?? null;
476-
if (!$existing || $field->fieldTypeInstance()->isEmpty($existing->value)) {
472+
$isEmpty = !$existing || $field->fieldTypeInstance()->isEmpty($existing->value);
473+
474+
// Non-translatable fields always come from primary language.
475+
// Translatable fields fall back only on the frontend (not in the admin editor,
476+
// where empty fields should stay empty so translators see what needs doing).
477+
if (!$field->translatable && $isEmpty) {
478+
$values[$field->id] = $primaryValues[$field->id];
479+
} elseif ($field->translatable && $isEmpty && Marble::translationFallbackEnabled()) {
477480
$values[$field->id] = $primaryValues[$field->id];
478481
}
479482
}

src/Resources/views/workflow/index.blade.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
@php $prefix = config('marble.route_prefix', 'admin'); @endphp
44

55
@section('content')
6-
<h1>{{ trans('marble::admin.workflows') }}</h1>
7-
6+
<div class="clearfix">
7+
<h1 class="pull-left">{{ trans('marble::admin.workflows') }}</h1>
8+
<div class="pull-right">
9+
<form method="POST" action="{{ url("{$prefix}/workflow/create") }}">
10+
@csrf
11+
<button type="submit" class="btn btn-success btn-sm">
12+
@include('marble::components.famicon', ['name' => 'add'])
13+
{{ trans('marble::admin.add_workflow') }}
14+
</button>
15+
</form>
16+
</div>
17+
</div>
818
<div class="main-box">
9-
<header class="main-box-header clearfix">
19+
<header class="main-box-header">
1020
<h2>{{ trans('marble::admin.workflows') }}</h2>
11-
<div class="pull-right" style="padding:10px">
12-
<form method="POST" action="{{ url("{$prefix}/workflow/create") }}">
13-
@csrf
14-
<button type="submit" class="btn btn-success btn-sm">
15-
@include('marble::components.famicon', ['name' => 'add'])
16-
{{ trans('marble::admin.add_workflow') }}
17-
</button>
18-
</form>
19-
</div>
2021
</header>
2122
<div class="main-box-body clearfix">
2223
@if($workflows->isEmpty())

0 commit comments

Comments
 (0)