Kreyu / data-table-bundle

Streamlines creation process of the data tables in Symfony applications. NOT PRODUCTION READY.
https://data-table-bundle.swroblewski.pl
MIT License
75 stars 14 forks source link

Removing label auto camelToSentence from ActionType #128

Closed rrr63 closed 1 month ago

rrr63 commented 1 month ago

Hello,

First, thanks for your bundle, it is amazing work ! I am testing it and it looks very good. I tried to create some rows action with customs labels :

$builder    
  ->addAction('Create a new Product', ButtonActionType::class, [
      'href' => $this->router->generate('app_product_new'),

  ])
  ->addRowAction('Editing a Product', ButtonActionType::class, [
      'href' => function (Product $product) {
          return $this->router->generate('app_product_edit', ['id' => $product->getId()]);
      },
  ])
  ->addRowAction('Deleting a Product', ButtonActionType::class, [
      'confirmation' => true,
      'href' => function (Product $product) {
          return $this->router->generate('app_product_delete', ['id' => $product->getId()]);
      },
  ]);

But when i wanted to render it :

with

it looks like it is always calling the camelToSentence function, but for label maybe we could let user select their own text ?

With this very small PR : without

Kreyu commented 1 month ago

Hey @rrr63, the conversion from camel case to sentence only happens if you haven't given a label.

The first argument of the addRowAction represents the name of the action (e.g. create, update, showDetails), similar to the Symfony Forms, where first argument of the add() method represents name of the field, not its label. Only if the label (docs) option is not given, the name gets converted to sentence case as a fallback (e.g. showDetails name fallbacks the label to Show details).

In your case, you most likely want something like:

$builder    
  ->addAction('create', ButtonActionType::class, [
      'label' => 'Create a new Product',
      'href' => $this->router->generate('app_product_new'),
  ])
  ->addRowAction('edit', ButtonActionType::class, [
      'label' => 'Editing a Product',
      'href' => function (Product $product) {
          return $this->router->generate('app_product_edit', ['id' => $product->getId()]);
      },
  ])
  ->addRowAction('delete', ButtonActionType::class, [
      'label' => 'Deleting a Product',
      'confirmation' => true,
      'href' => function (Product $product) {
          return $this->router->generate('app_product_delete', ['id' => $product->getId()]);
      },
  ])
;

or skip the label option and use translation to handle the labels (you can set translation domain in configureOptions like in forms):

// in your data table type class
public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefault('translation_domain', 'product');
}
# translations/product.en.yaml
create: Create a new Product
edit: Editing a Product
delete: Deleting a Product

The similar configuration works for other components - columns, filters and exporters.

rrr63 commented 1 month ago

Hey @rrr63, the conversion from camel case to sentence only happens if you haven't given a label.

The first argument of the addRowAction represents the name of the action (e.g. create, update, showDetails), similar to the Symfony Forms, where first argument of the add() method represents name of the field, not its label. Only if the label (docs) option is not given, the name gets converted to sentence case as a fallback (e.g. showDetails name fallbacks the label to Show details).

In your case, you most likely want something like:

$builder    
  ->addAction('create', ButtonActionType::class, [
      'label' => 'Create a new Product',
      'href' => $this->router->generate('app_product_new'),
  ])
  ->addRowAction('edit', ButtonActionType::class, [
      'label' => 'Editing a Product',
      'href' => function (Product $product) {
          return $this->router->generate('app_product_edit', ['id' => $product->getId()]);
      },
  ])
  ->addRowAction('delete', ButtonActionType::class, [
      'label' => 'Deleting a Product',
      'confirmation' => true,
      'href' => function (Product $product) {
          return $this->router->generate('app_product_delete', ['id' => $product->getId()]);
      },
  ])
;

or skip the label option and use translation to handle the labels (you can set translation domain in configureOptions like in forms):

// in your data table type class
public function configureOptions(OptionsResolver $resolver): void
{
    $resolver->setDefault('translation_domain', 'product');
}
# translations/product.en.yaml
create: Create a new Product
edit: Editing a Product
delete: Deleting a Product

The similar configuration works for other components - columns, filters and exporters.

Thanks ! It was that exactly 👍