Skip to content

Commit 0363891

Browse files
committed
IBX-4675: Improved autogenerate field definition name
1 parent 59bad30 commit 0363891

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ parameters:
385385
count: 1
386386
path: src/bundle/Controller/ContentViewController.php
387387

388+
-
389+
message: "#^Parameter \\#1 \\$fieldTypeIdentifier of method Ibexa\\\\Bundle\\\\AdminUi\\\\Controller\\\\FieldDefinitionController\\:\\:getFieldTypeLabel\\(\\) expects string, string\\|null given\\.$#"
390+
count: 1
391+
path: src/bundle/Controller/FieldDefinitionController.php
392+
388393
-
389394
message: "#^Parameter \\#2 \\$fieldDefinition of method Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\ContentTypeService\\:\\:updateFieldDefinition\\(\\) expects Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition, Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition\\|null given\\.$#"
390395
count: 1

src/bundle/Controller/FieldDefinitionController.php

+30-7
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,28 @@
1919
use Ibexa\Rest\Message;
2020
use Ibexa\Rest\Server\Controller as RestController;
2121
use Ibexa\Rest\Server\Values;
22+
use JMS\TranslationBundle\Annotation\Ignore;
2223
use Symfony\Component\HttpFoundation\RedirectResponse;
2324
use Symfony\Component\HttpFoundation\Request;
2425
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26+
use Symfony\Contracts\Translation\TranslatorInterface;
2527

2628
final class FieldDefinitionController extends RestController
2729
{
28-
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
29-
private $contentTypeService;
30+
private ContentTypeService $contentTypeService;
3031

31-
/** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
32-
private $urlGenerator;
32+
private UrlGeneratorInterface $urlGenerator;
3333

34-
public function __construct(ContentTypeService $contentTypeService, UrlGeneratorInterface $urlGenerator)
35-
{
34+
private TranslatorInterface $translator;
35+
36+
public function __construct(
37+
ContentTypeService $contentTypeService,
38+
UrlGeneratorInterface $urlGenerator,
39+
TranslatorInterface $translator
40+
) {
3641
$this->contentTypeService = $contentTypeService;
3742
$this->urlGenerator = $urlGenerator;
43+
$this->translator = $translator;
3844
}
3945

4046
public function addFieldDefinitionAction(
@@ -59,7 +65,9 @@ public function addFieldDefinitionAction(
5965

6066
$fieldDefinitionCreateStruct->fieldGroup = $input->fieldGroupIdentifier;
6167
$fieldDefinitionCreateStruct->names = [
62-
$language->languageCode => 'New field type',
68+
$language->languageCode => strtr('New %name%', [
69+
'%name%' => $this->getFieldTypeLabel($input->fieldTypeIdentifier),
70+
]),
6371
];
6472

6573
$fieldDefinitionCreateStruct->position = $input->position ?? $this->getNextFieldPosition($contentTypeDraft);
@@ -167,4 +175,19 @@ private function getNextFieldPosition(ContentType $contentType): int
167175

168176
return 0;
169177
}
178+
179+
/**
180+
* Generate a human-readable name for field type identifier.
181+
*/
182+
private function getFieldTypeLabel(string $fieldTypeIdentifier): string
183+
{
184+
$name = $this->translator->trans(
185+
/** @Ignore */
186+
$fieldTypeIdentifier . '.name',
187+
[],
188+
'ibexa_fieldtypes'
189+
);
190+
191+
return mb_strtolower($name);
192+
}
170193
}

src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,9 @@
459459
const targetContainerGroup = targetContainer.closest('.ibexa-collapse--field-definitions-group');
460460
const targetContainerList = targetContainerGroup.closest('.ibexa-content-type-edit__field-definitions-group-list');
461461
const fieldTemplate = targetContainerList.dataset.template;
462-
const fieldRendered = fieldTemplate.replace('{{ type }}', currentDraggedItem.dataset.itemIdentifier);
462+
const fieldRendered = fieldTemplate
463+
.replace('{{ name }}', currentDraggedItem.dataset.itemName.toLowerCase())
464+
.replace('{{ type }}', currentDraggedItem.dataset.itemIdentifier);
463465
let draggedItemPosition = [...dragContainerItems].findIndex((item, index, array) => {
464466
return item.classList.contains('ibexa-field-definitions-placeholder') && index < array.length - 1;
465467
});

src/bundle/Resources/translations/ibexa_content_type.en.xliff

+2-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@
322322
<note>key: content_type.view.edit.content_field_definitions</note>
323323
</trans-unit>
324324
<trans-unit id="74aead78c735bbbce032dc9b00f9ca7a3632f166" resname="content_type.view.edit.default_header">
325-
<source>New field type</source>
326-
<target state="new">New field type</target>
325+
<source>New {{ name }} ({{ type }})</source>
326+
<target state="new">New {{ name }} ({{ type }})</target>
327327
<note>key: content_type.view.edit.default_header</note>
328328
</trans-unit>
329329
<trans-unit id="a32a5e3d2d67da43b4c5f0f5574c90520868b36a" resname="content_type.view.edit.global_properties">

src/bundle/Resources/views/themes/admin/content_type/available_field_types.html.twig

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
{% for item in field_type_toolbar %}
1919
<li
2020
class="ibexa-available-field-type {{ is_draggable is defined and is_draggable == false ? 'ibexa-available-field-type--immovable' }}"
21+
data-item-name="{{ item.name }}"
2122
data-item-identifier="{{ item.identifier }}"
2223
>
2324
<div

src/bundle/Resources/views/themes/admin/content_type/field_definitions.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
'is_draggable': true,
1717
'class': 'ibexa-collapse--field-definition ibexa-collapse--field-definition-highlight ibexa-collapse--field-definition-loading',
1818
'body_id': 'loading-collapse',
19-
'header_label': 'content_type.view.edit.default_header'|trans|desc('New field type') ~ ' ({{ type }})',
19+
'header_label': 'content_type.view.edit.default_header'|trans|desc('New {{ name }} ({{ type }})'),
2020
'data_attr': {
2121
'data-field-definition-identifier': 'loading-collapse',
2222
},

0 commit comments

Comments
 (0)