Skip to content

Removing label auto camelToSentence from ActionType #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

rrr63
Copy link

@rrr63 rrr63 commented Sep 17, 2024

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
Copy link
Owner

Kreyu commented Sep 17, 2024

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.

@Kreyu Kreyu closed this Sep 17, 2024
@rrr63 rrr63 deleted the fix-uppercase-name branch September 17, 2024 14:38
@rrr63
Copy link
Author

rrr63 commented Sep 17, 2024

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants