Skip to content

Commit 49944be

Browse files
committed
feat: unset module translations if no translatable fields for types
1 parent 2218a6e commit 49944be

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Controller/Component/ModulesComponent.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,33 @@ function (&$data, $key) use ($metaModules): void {
166166
if (!$this->Schema->tagsInUse()) {
167167
unset($this->modules['tags']);
168168
}
169+
$types = array_keys($this->modules);
170+
if (isset($this->modules['translations']) && !$this->translationsEnabled($types)) {
171+
unset($this->modules['translations']);
172+
}
169173

170174
return $this->modules;
171175
}
172176

177+
/**
178+
* Check if translations are enabled for at least one of the given object types.
179+
*
180+
* @param array $types Object types to check.
181+
* @return bool
182+
*/
183+
public function translationsEnabled(array $types): bool
184+
{
185+
foreach ($types as $objectType) {
186+
$schema = (array)$this->Schema->getSchema($objectType);
187+
$translatable = (array)Hash::get($schema, 'translatable');
188+
if (count($translatable) > 0) {
189+
return true;
190+
}
191+
}
192+
193+
return false;
194+
}
195+
173196
/**
174197
* This filters modules and apply 'AccessControl' config by user role, if any.
175198
* Module can be "hidden": remove from modules.

tests/TestCase/Controller/Component/ModulesComponentTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#[CoversMethod(ModulesComponent::class, 'skipSavePermissions')]
7373
#[CoversMethod(ModulesComponent::class, 'skipSaveRelated')]
7474
#[CoversMethod(ModulesComponent::class, 'startup')]
75+
#[CoversMethod(ModulesComponent::class, 'translationsEnabled')]
7576
#[CoversMethod(ModulesComponent::class, 'upload')]
7677
class ModulesComponentTest extends TestCase
7778
{
@@ -588,6 +589,18 @@ function ($param) use ($meta, $modules) {
588589
return compact('meta');
589590
},
590591
);
592+
$apiClient->method('schema')
593+
->willReturnCallback(
594+
function ($type) {
595+
if ($type === 'bedita') {
596+
return [
597+
'translatable' => ['title', 'body'],
598+
];
599+
}
600+
601+
return [];
602+
},
603+
);
591604
}
592605
ApiClientProvider::setApiClient($apiClient);
593606

0 commit comments

Comments
 (0)