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 callable field #321

Merged
merged 8 commits into from
Feb 10, 2025
Merged

Add callable field #321

merged 8 commits into from
Feb 10, 2025

Conversation

Florian-Merle
Copy link
Contributor

@Florian-Merle Florian-Merle commented Mar 22, 2024

The Callable column aims to offer almost as much flexibility as the Twig column, but without requiring the creation of a template.
You simply need to specify a callable, which allows you to transform the 'data' variable on the fly.

The documentation associated for this PR can be found here Sylius/Stack#228

@Florian-Merle Florian-Merle force-pushed the callback-field branch 4 times, most recently from 228c8fe to 6a1c438 Compare March 22, 2024 15:48
By default it uses the name of the field, but you can specify the path
alternatively. For example:

<details open><summary>PHP</summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match existing documentation, please add YAML example as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not include documentation for the YAML format because I believe it's impossible to define a callback field in this format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature is not complete then and cannot be merged until YAML support will be dropped.

Here is example on how callback can be implemented with non-php config format.

https://symfony.com/doc/current/reference/constraints/Callback.html#external-callbacks-and-closures

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do it. The repository can be a callback in YAML.

app_book_by_english_authors:
    driver:
        name: doctrine/orm
        options:
            class: App\Entity\Book
            repository:
                method: [expr:service('app.english_books_query_builder'), create]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the feature and introduced a new way to define callbacks, inspired by Loic's comment:

sylius_grid:
    grids:
        app_user:
            fields:
                name:
                    type: callback
                    options:
                        callback: "callback:strtoupper"
                    label: app.ui.name

This approach allows string callables to be defined directly in YAML.
When configuring grids using PHP, both string and array callables are supported.
When configuring grids using service definitions, everything is supported: string callables, array callables, and closures.

I’ve documented this change here: SyliusGridBundle PR #321.

To implement this, I copied the OptionParser class from the ResourceBundle, slimming it down to only what’s necessary for this feature (handling callbacks). See the original class: OptionsParser in ResourceBundle.

Since the resource bundle is not a required dependencie, I think this is fine to copy the class. However having two OptionParser might not be ideal. Please let me know what you think about it.

@Florian-Merle Florian-Merle changed the base branch from 1.13 to 1.14 January 21, 2025 14:29
@Florian-Merle Florian-Merle force-pushed the callback-field branch 3 times, most recently from 11c567d to bf89482 Compare January 21, 2025 14:53
@Florian-Merle
Copy link
Contributor Author

Florian-Merle commented Jan 21, 2025

After some thinking, I believe it would be more appropriate to rename CallbackField into CallableField. I'll refactor that. -> this has been done

@Florian-Merle Florian-Merle changed the title add callback field add callable field Jan 21, 2025
@loic425 loic425 changed the title add callable field Add callable field Feb 5, 2025
Copy link
Member

@diimpp diimpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GSadee GSadee added the Feature New feature proposals. label Feb 10, 2025
@GSadee GSadee merged commit 9bec850 into Sylius:1.14 Feb 10, 2025
10 checks passed
@GSadee
Copy link
Member

GSadee commented Feb 10, 2025

Thank you @Florian-Merle! 🎉

@Florian-Merle Florian-Merle deleted the callback-field branch February 10, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New feature proposals.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants