sonata-project / SonataCoreBundle

[deprecated] SonataCoreBundle
MIT License
318 stars 138 forks source link

Add a preprocess callback in ListMapper::add() to make it easier to tweak displayed values #673

Closed quazardous closed 5 years ago

quazardous commented 5 years ago

Feature Request

Could be very handy to have a quick way to preprocess the value before display in the list.

What I do now to tweak

$listMapper
            ->add('type', 'trans', ['code' => 'extract', 'parameters' => [function (Purchase $purchase) {
                return 'purchase.type_' . $purchase->getType();
            }], 'catalogue' => 'messages'])
...
// having this in my entities
...
    public function extract($extractorCallback, ...$args)
    {
        array_unshift($args, $this);
        return call_user_func_array($extractorCallback, $args);
    }

What could be cool

$listMapper
            ->add('type', 'trans', ['preprocess' => function ($value) {
                return 'purchase.type_' . $value;
            }], 'catalogue' => 'messages'])
core23 commented 5 years ago

Can you please elaborate a little bit more what you would like to do.

Bonus points if you create a PR with the feature you need 💯

quazardous commented 5 years ago

I want to be able to apply a function on the field raw value before it is passed to the normal flow.

for translate

$listMapper ->add('type', 'trans', [], 'catalogue' => 'messages']);


without preprocess the translate file for types would be like
```php
# root level
type1: Type 1
type2: Type 2
...

with some preprocess

$listMapper ->add('type', 'trans', ['preprocess' => function ($value) { return 'purchase.type_' . $value; }], 'catalogue' => 'messages']);



the translate file for types would be like
```php
# root level
purchase
    type_type1: Type 1
    type_type2: Type 2
...

This is not a big feature, but it can be handy too when you need to beautify raw value and overriding a template is little too much.

If I have some time I will try to do a PR
core23 commented 5 years ago

This bundle is deprecated. Please create a new feature request at the https://github.com/sonata-project/form-extensions project