Skip to content
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

Add label strategies #161

Open
Kreyu opened this issue Jan 4, 2025 · 0 comments
Open

Add label strategies #161

Kreyu opened this issue Jan 4, 2025 · 0 comments
Labels
feature New feature that would fit the core bundle

Comments

@Kreyu
Copy link
Owner

Kreyu commented Jan 4, 2025

By default, the column/filter/action/exporter labels are inherited from the name, e.g. "firstName" will result in "First name". In some cases it may be useful to change this behavior, so adding label strategies like the SonataAdmin does would be cool.

Example strategies (just ideas for now):

  • sentence - current behavior, e.g. "firstName" becomes "First name", for simple applications;
  • snake - "firstName" -> "first_name", for more complex applications using i18n;
  • format - requires a format string supported by sprintf, e.g. "data_table.%s"
  • chain - allows usage of multiple strategies;
  • noop - "firstName" -> "firstName" (string not modified);

For now, the label strategy should be configurable for the data table from its options, for example:

class ProductDataTableType extends AbstractDataTableType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'label_strategy' => new ChainLabelStrategy([
                new SnakeLabelStrategy(),
                new FormatLabelStrategy('data_table.%s'),
            ]),
        ]);
    }
}

With above configuration, the "firstName" column would use the "data_table.first_name" label by default.

This could be easily configured for the whole application by using an extension:

class LabelStrategyDataTableTypeExtension extends AbstractDataTableTypeExtension
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'label_strategy' => new ChainLabelStrategy([
                new SnakeLabelStrategy(),
                new FormatLabelStrategy('data_table.%s'),
            ]),
        ]);
    }

    public static function getExtendedTypes(): iterable
    {
        return [DataTableType::class];
    }
}
@Kreyu Kreyu added the feature New feature that would fit the core bundle label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature that would fit the core bundle
Projects
None yet
Development

No branches or pull requests

1 participant