Skip to content

Commit 859a8a4

Browse files
authored
fix(forms): Display translations already defined even when the original value is empty (#21762)
1 parent a2dd95b commit 859a8a4

File tree

16 files changed

+459
-180
lines changed

16 files changed

+459
-180
lines changed

src/Entity.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,10 +3385,7 @@ public function listTranslationsHandlers(): array
33853385
$handlers = [];
33863386
$key = sprintf('%s_%d', self::getType(), $this->getID());
33873387
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
3388-
if (
3389-
!empty($this->fields['custom_helpdesk_home_title'])
3390-
&& $this->fields['custom_helpdesk_home_title'] != self::CONFIG_PARENT
3391-
) {
3388+
if ($this->fields['custom_helpdesk_home_title'] != self::CONFIG_PARENT) {
33923389
$handlers[$key][] = new TranslationHandler(
33933390
item: $this,
33943391
key: self::TRANSLATION_KEY_CUSTOM_HELPDESK_HOME_TITLE,

src/Glpi/Form/Comment.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,23 @@ public function listTranslationsHandlers(): array
179179
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
180180
$handlers = [];
181181

182-
if (!empty($this->fields['name'])) {
183-
$handlers[$key][] = new TranslationHandler(
184-
item: $this,
185-
key: self::TRANSLATION_KEY_NAME,
186-
name: __('Comment title'),
187-
value: $this->fields['name'],
188-
is_rich_text: false,
189-
category: $category_name
190-
);
191-
}
182+
$handlers[$key][] = new TranslationHandler(
183+
item: $this,
184+
key: self::TRANSLATION_KEY_NAME,
185+
name: __('Comment title'),
186+
value: $this->fields['name'],
187+
is_rich_text: false,
188+
category: $category_name
189+
);
192190

193-
if (!empty($this->fields['description'])) {
194-
$handlers[$key][] = new TranslationHandler(
195-
item: $this,
196-
key: self::TRANSLATION_KEY_DESCRIPTION,
197-
name: __('Comment description'),
198-
value: $this->fields['description'],
199-
is_rich_text: true,
200-
category: $category_name
201-
);
202-
}
191+
$handlers[$key][] = new TranslationHandler(
192+
item: $this,
193+
key: self::TRANSLATION_KEY_DESCRIPTION,
194+
name: __('Comment description'),
195+
value: $this->fields['description'],
196+
is_rich_text: true,
197+
category: $category_name
198+
);
203199

204200
return $handlers;
205201
}

src/Glpi/Form/Form.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -451,38 +451,32 @@ public function listTranslationsHandlers(): array
451451
$key = sprintf('%s_%d', self::getType(), $this->getID());
452452
$category_name = __('Form properties');
453453
$handlers = [];
454-
if (!empty($this->fields['name'])) {
455-
$handlers[$key][] = new TranslationHandler(
456-
item: $this,
457-
key: self::TRANSLATION_KEY_NAME,
458-
name: __('Form title'),
459-
value: $this->fields['name'],
460-
is_rich_text: false,
461-
category: $category_name
462-
);
463-
}
454+
$handlers[$key][] = new TranslationHandler(
455+
item: $this,
456+
key: self::TRANSLATION_KEY_NAME,
457+
name: __('Form title'),
458+
value: $this->fields['name'],
459+
is_rich_text: false,
460+
category: $category_name
461+
);
464462

465-
if (!empty($this->fields['header'])) {
466-
$handlers[$key][] = new TranslationHandler(
467-
item: $this,
468-
key: self::TRANSLATION_KEY_HEADER,
469-
name: __('Form description'),
470-
value: $this->fields['header'],
471-
is_rich_text: true,
472-
category: $category_name
473-
);
474-
}
463+
$handlers[$key][] = new TranslationHandler(
464+
item: $this,
465+
key: self::TRANSLATION_KEY_HEADER,
466+
name: __('Form description'),
467+
value: $this->fields['header'],
468+
is_rich_text: true,
469+
category: $category_name
470+
);
475471

476-
if (!empty($this->fields['description'])) {
477-
$handlers[$key][] = new TranslationHandler(
478-
item: $this,
479-
key: self::TRANSLATION_KEY_DESCRIPTION,
480-
name: __('Service catalog description'),
481-
value: $this->fields['description'],
482-
is_rich_text: true,
483-
category: $category_name
484-
);
485-
}
472+
$handlers[$key][] = new TranslationHandler(
473+
item: $this,
474+
key: self::TRANSLATION_KEY_DESCRIPTION,
475+
name: __('Service catalog description'),
476+
value: $this->fields['description'],
477+
is_rich_text: true,
478+
category: $category_name
479+
);
486480

487481
$sections_handlers = array_map(
488482
fn($section) => $section->listTranslationsHandlers(),

src/Glpi/Form/Migration/FormMigration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ private function processMigrationOfTranslations(): void
13051305

13061306
foreach ($form->listTranslationsHandlers() as $handlers) {
13071307
foreach ($handlers as $handler) {
1308-
if (isset($translations[$handler->getValue()])) {
1308+
if (!empty($handler->getValue()) && isset($translations[$handler->getValue()])) {
13091309
$this->importItem(
13101310
FormTranslation::class,
13111311
[

src/Glpi/Form/Question.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,23 @@ public function listTranslationsHandlers(): array
135135
$key = sprintf('%s_%d', self::getType(), $this->getID());
136136
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
137137
$handlers = [];
138-
if (!empty($this->fields['name'])) {
139-
$handlers[$key][] = new TranslationHandler(
140-
item: $this,
141-
key: self::TRANSLATION_KEY_NAME,
142-
name: __('Question name'),
143-
value: $this->fields['name'],
144-
is_rich_text: false,
145-
category: $category_name
146-
);
147-
}
138+
$handlers[$key][] = new TranslationHandler(
139+
item: $this,
140+
key: self::TRANSLATION_KEY_NAME,
141+
name: __('Question name'),
142+
value: $this->fields['name'],
143+
is_rich_text: false,
144+
category: $category_name
145+
);
148146

149-
if (!empty($this->fields['description'])) {
150-
$handlers[$key][] = new TranslationHandler(
151-
item: $this,
152-
key: self::TRANSLATION_KEY_DESCRIPTION,
153-
name: __('Question description'),
154-
value: $this->fields['description'],
155-
is_rich_text: true,
156-
category: $category_name
157-
);
158-
}
147+
$handlers[$key][] = new TranslationHandler(
148+
item: $this,
149+
key: self::TRANSLATION_KEY_DESCRIPTION,
150+
name: __('Question description'),
151+
value: $this->fields['description'],
152+
is_rich_text: true,
153+
category: $category_name
154+
);
159155

160156
$question_type = $this->getQuestionType();
161157
if ($question_type instanceof TranslationAwareQuestionType) {

src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,17 @@ public function convertExtraData(array $rawData): array
243243
#[Override]
244244
public function listTranslationsHandlers(Question $question): array
245245
{
246-
$handlers = [];
247246
$options = $this->getOptions($question);
248-
if ($options !== []) {
249-
$handlers = array_map(
250-
fn($uuid, $option) => new TranslationHandler(
251-
item: $question,
252-
key: sprintf('%s-%s', self::TRANSLATION_KEY_OPTION, $uuid),
253-
name: sprintf('%s %s', self::getName(), __('Option')),
254-
value: $option,
255-
),
256-
array_keys($options),
257-
$options
258-
);
259-
}
247+
$handlers = array_map(
248+
fn($uuid, $option) => new TranslationHandler(
249+
item: $question,
250+
key: sprintf('%s-%s', self::TRANSLATION_KEY_OPTION, $uuid),
251+
name: sprintf('%s %s', self::getName(), __('Option')),
252+
value: $option,
253+
),
254+
array_keys($options),
255+
$options
256+
);
260257

261258
return $handlers;
262259
}

src/Glpi/Form/QuestionType/QuestionTypeLongText.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,13 @@ public function listTranslationsHandlers(Question $question): array
227227
{
228228
$handlers = [];
229229

230-
if (!empty($question->fields['default_value'])) {
231-
$handlers[] = new TranslationHandler(
232-
item: $question,
233-
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
234-
name: __('Default value'),
235-
value: $question->fields['default_value'],
236-
is_rich_text: true
237-
);
238-
}
230+
$handlers[] = new TranslationHandler(
231+
item: $question,
232+
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
233+
name: __('Default value'),
234+
value: $question->fields['default_value'],
235+
is_rich_text: true
236+
);
239237

240238
return $handlers;
241239
}

src/Glpi/Form/QuestionType/QuestionTypeShortText.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,12 @@ public function getConditionHandlers(
8080
#[Override]
8181
public function listTranslationsHandlers(Question $question): array
8282
{
83-
$default_value = $question->fields['default_value'] ?? '';
84-
if (empty($default_value)) {
85-
return [];
86-
}
87-
8883
return [
8984
new TranslationHandler(
9085
item: $question,
9186
key: Question::TRANSLATION_KEY_DEFAULT_VALUE,
9287
name: __('Default value'),
93-
value: $default_value,
88+
value: $question->fields['default_value'] ?? '',
9489
),
9590
];
9691
}

src/Glpi/Form/Section.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,23 @@ public function listTranslationsHandlers(): array
159159
$key = sprintf('%s_%d', self::getType(), $this->getID());
160160
$category_name = sprintf('%s: %s', self::getTypeName(), $this->getName());
161161
if (count($form->getSections()) > 1) {
162-
if (!empty($this->fields['name'])) {
163-
$handlers[$key][] = new TranslationHandler(
164-
item: $this,
165-
key: self::TRANSLATION_KEY_NAME,
166-
name: __('Section title'),
167-
value: $this->fields['name'],
168-
is_rich_text: false,
169-
category: $category_name
170-
);
171-
}
162+
$handlers[$key][] = new TranslationHandler(
163+
item: $this,
164+
key: self::TRANSLATION_KEY_NAME,
165+
name: __('Section title'),
166+
value: $this->fields['name'],
167+
is_rich_text: false,
168+
category: $category_name
169+
);
172170

173-
if (!empty($this->fields['description'])) {
174-
$handlers[$key][] = new TranslationHandler(
175-
item: $this,
176-
key: self::TRANSLATION_KEY_DESCRIPTION,
177-
name: __('Section description'),
178-
value: $this->fields['description'],
179-
is_rich_text: true,
180-
category: $category_name
181-
);
182-
}
171+
$handlers[$key][] = new TranslationHandler(
172+
item: $this,
173+
key: self::TRANSLATION_KEY_DESCRIPTION,
174+
name: __('Section description'),
175+
value: $this->fields['description'],
176+
is_rich_text: true,
177+
category: $category_name
178+
);
183179
}
184180

185181
$blocks_handlers = [];

src/Glpi/Helpdesk/HelpdeskTranslation.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Glpi\Application\View\TemplateRenderer;
4242
use Glpi\Helpdesk\Tile\TilesManager;
4343
use Glpi\ItemTranslation\Context\ProvideTranslationsInterface;
44+
use Glpi\ItemTranslation\Context\TranslationHandler;
4445
use Glpi\ItemTranslation\ItemTranslation;
4546
use Override;
4647

@@ -125,7 +126,7 @@ public function listTranslationsHandlers(): array
125126
$tiles_manager = TilesManager::getInstance();
126127
$entities = array_map(
127128
fn($entity_id) => Entity::getById($entity_id),
128-
array_keys((new Entity())->find())
129+
array_keys((new Entity())->find(getEntitiesRestrictCriteria(Entity::getTable())))
129130
);
130131
$entities_handlers = array_map(
131132
fn($entity) => $entity->listTranslationsHandlers(),
@@ -183,7 +184,18 @@ public static function getLanguagesCanBeAddedToTranslation(): array
183184
#[Override]
184185
protected function getTranslationsHandlersForStats(): array
185186
{
186-
return $this->listTranslationsHandlers();
187+
// Filter out handlers with empty values and those that do not have a translation yet
188+
return array_map(
189+
fn(array $handlers) => array_filter(
190+
$handlers,
191+
fn(TranslationHandler $handler) => !empty($handler->getValue()) || !empty(self::getForItemKeyAndLanguage(
192+
$handler->getItem(),
193+
$handler->getKey(),
194+
$this->fields['language']
195+
)?->getTranslation())
196+
),
197+
$this->listTranslationsHandlers()
198+
);
187199
}
188200

189201
public static function getSystemSQLCriteria(?string $tablename = null): array

0 commit comments

Comments
 (0)