Improved UX of entity adding from part edit page.

This commit is contained in:
Jan Böhmer 2023-01-30 23:08:22 +01:00
parent 25be76b311
commit dc012b56a8
4 changed files with 31 additions and 20 deletions

View file

@ -34,21 +34,29 @@ export default class extends Controller {
this._emptyMessage = this.element.getAttribute("data-empty-message") ?? "";
const allowAdd = this.element.getAttribute("data-allow-add") === "true";
const addHint = this.element.getAttribute("data-add-hint") ?? "";
let settings = {
allowEmptyOption: true,
selectOnTab: true,
maxOptions: null,
create: allowAdd,
createFilter: /\D/, //Must contain a non-digit character, otherwise they would be recognized as DB ID
searchField: [
{field: "text", weight : 2},
{field: "parent", weight : 0.5},
{field: "path", weight : 1.0},
],
render: {
item: this.renderItem.bind(this),
option: this.renderOption.bind(this),
option_create: function(data, escape) {
return '<div class="create">Add <strong>' + escape(data.input) + '</strong>&hellip;&nbsp;' +
'<small class="text-muted float-end">(' + addHint +')</small>' +
'</div>';
},
}
};

View file

@ -32,6 +32,7 @@ use RuntimeException;
use Symfony\Component\Intl\Currencies;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* An entity to select a currency shortly
@ -40,9 +41,9 @@ class CurrencyEntityType extends StructuralEntityType
{
protected ?string $base_currency;
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, ?string $base_currency)
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator, ?string $base_currency)
{
parent::__construct($em, $builder, $attachmentURLGenerator);
parent::__construct($em, $builder, $attachmentURLGenerator, $translator);
$this->base_currency = $base_currency;
}

View file

@ -44,6 +44,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\IsNull;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* This class provides a choice form type similar to EntityType, with the difference, that the tree structure
@ -53,17 +54,19 @@ class StructuralEntityType extends AbstractType
{
protected EntityManagerInterface $em;
protected AttachmentURLGenerator $attachmentURLGenerator;
protected TranslatorInterface $translator;
/**
* @var NodesListBuilder
*/
protected $builder;
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator)
public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, TranslatorInterface $translator)
{
$this->em = $em;
$this->builder = $builder;
$this->attachmentURLGenerator = $attachmentURLGenerator;
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
@ -85,21 +88,6 @@ class StructuralEntityType extends AbstractType
}
});
/* $builder->addEventListener(FormEvents::POST_SUBMIT, function (PostSubmitEvent $event) {
$name = $event->getForm()->getConfig()->getName();
$data = $event->getForm()->getData();
if ($event->getForm()->getParent() === null) {
return;
}
$event->getForm()->getParent()->add($name, static::class, $event->getForm()->getConfig()->getOptions());
$new_form = $event->getForm()->getParent()->get($name);
$new_form->setData($data);
});*/
$builder->addModelTransformer(new CallbackTransformer(
function ($value) use ($options) {
return $this->modelTransform($value, $options);
@ -145,7 +133,7 @@ class StructuralEntityType extends AbstractType
{
//Show entities that are not added to DB yet separately from other entities
if ($element->getID() === null) {
return 'New (not added to DB yet)';
return $this->translator->trans('entity.select.group.new_not_added_to_DB');
}
return null;
@ -166,10 +154,11 @@ class StructuralEntityType extends AbstractType
$resolver->setDefault('controller', 'elements--structural-entity-select');
$resolver->setDefault('attr', static function (Options $options) {
$resolver->setDefault('attr', function (Options $options) {
$tmp = [
'data-controller' => $options['controller'],
'data-allow-add' => $options['allow_add'] ? 'true' : 'false',
'data-add-hint' => $this->translator->trans('entity.select.add_hint'),
];
if ($options['empty_message']) {
$tmp['data-empty-message'] = $options['empty_message'];
@ -236,6 +225,7 @@ class StructuralEntityType extends AbstractType
$tmp += [
'data-level' => $level,
'data-parent' => $choice->getParent() ? $choice->getParent()->getFullPath() : null,
'data-path' => $choice->getFullPath('->'),
'data-image' => $choice->getMasterPictureAttachment() ? $this->attachmentURLGenerator->getThumbnailURL($choice->getMasterPictureAttachment(), 'thumbnail_xs') : null,
];

View file

@ -10416,5 +10416,17 @@ Element 3</target>
<target>A PCRE-compatible regular expression, which a part name have to match.</target>
</segment>
</unit>
<unit id="vr7oZKL" name="entity.select.add_hint">
<segment>
<source>entity.select.add_hint</source>
<target><![CDATA[Use -> to create nested structures, e.g. "Node 1->Node 1.1"]]></target>
</segment>
</unit>
<unit id="_cXCaLo" name="entity.select.group.new_not_added_to_DB">
<segment>
<source>entity.select.group.new_not_added_to_DB</source>
<target>New (not added to DB yet)</target>
</segment>
</unit>
</file>
</xliff>