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

[4.x] Allow custom form exporters to be defined #8837

Merged

Conversation

ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Oct 13, 2023

This PR allows custom form exporters to be defined in a new statamic.forms.exporters config key.

    'exporters' => [
        'csv' => [
            'class' => Statamic\Forms\Exporters\CsvExporter::class,
            'label' => 'Export as CSV',
        ],
        'json' => [
            'class' => Statamic\Forms\Exporters\JsonExporter::class,
            'label' => 'Export as JSON',
        ],
    ],

Say for example you wanted to add your own Excel exporter:

    'exporters' => [
        'csv' => [
            'class' => Statamic\Forms\Exporters\CsvExporter::class,
            'title' => 'Export as CSV',
        ],
        'json' => [
            'class' => Statamic\Forms\Exporters\JsonExporter::class,
            'title' => 'Export as JSON',
        ],
        'excel' => [
            'class' => App\Exporters\ExcelExporter::class,
            'title' => 'Export as Excel',
            'some_config_setting_i_need' => 100,
        ],
    ],

I've modified both the exporter contract and abstract class to allow config values to be set.

You can also specify a forms key in the config, and it will limit the exporter to those form handles.

Closes statamic/ideas#834

@ryanmitchell ryanmitchell changed the title Allow custom form exporters to be defined in config [4.x] Allow custom form exporters to be defined in config Oct 13, 2023
@ryanmitchell ryanmitchell changed the title [4.x] Allow custom form exporters to be defined in config [4.x] Allow custom form exporters to be defined Oct 13, 2023
@marcorieser
Copy link
Contributor

This is huge! It would be nice if there was an option to limit exports to specific forms.

@ryanmitchell
Copy link
Contributor Author

@marcorieser sure, easily added. You specify a forms array to limit them to certain handles.

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

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

Great idea. Just have a little different direction in mind.

resources/views/forms/show.blade.php Outdated Show resolved Hide resolved
src/Forms/Exporters/AbstractExporter.php Outdated Show resolved Hide resolved
src/Http/Controllers/CP/Forms/FormExportController.php Outdated Show resolved Hide resolved
src/Contracts/Forms/Exporter.php Outdated Show resolved Hide resolved
src/Forms/Exporters/AbstractExporter.php Outdated Show resolved Hide resolved
src/Contracts/Forms/Exporter.php Outdated Show resolved Hide resolved
config/forms.php Show resolved Hide resolved
@ryanmitchell
Copy link
Contributor Author

All changes made as requested. I also made it possible in the CSV exporter to define csv_delimiter and csv_headers in the config for the exporter - so you could potentially change it per form if thats wanted. Maybe at some point those should be removed from the root level of the config.

- Add types
- Remove docblocks
- Remove exporter interface in favor of forcing the abstract class, and rename it.
- Remove exporter form method, just use the property.
- Separate getter/setter methods.
- Form can get its own exporters
- Clean up blade by calling exporter methods
- Clean up controller by moving logic into exporter class and repo
- Just 404 on invalid types rather than exceptions
- You can override the exporter title in the config
- Exporter has title method that translates
@jasonvarga jasonvarga merged commit 4a753dc into statamic:4.x Oct 30, 2023
17 checks passed
@ryanmitchell ryanmitchell deleted the feature/allow-custom-form-exporters branch October 30, 2023 21:19
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.

Custom form submission exporters
3 participants