Skip to content

Commit

Permalink
Field parser docs
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Mar 8, 2024
1 parent cf3b929 commit 5ca460a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
41 changes: 40 additions & 1 deletion docs/custom-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,43 @@ prev: false
description: Documentation for Element Exporter, a plugin for Craft CMS.
---

# Register field support

# Registering support for custom fields

The plugin comes with a number of built in parsers, all following the [BaseFieldParser](https://github.com/studioespresso/craft-exporter/blob/develop/src/fields/BaseFieldParser.php) class.

Each field is assigned a parser, with the PlainTextParser acting as a backup in case the selected parser can't return the value.

Registering plugin fields to a parser - either a built parser or your own, can be done through the ``EVENT_REGISTER_EXPORTABLE_FIELD_TYPES`` event.

In the example below, we add Formie's `Checkboxes` field to the built-in `MultiOptionsFieldParser::class`.


````php
use studioespresso\exporter\helpers\FieldTypeHelper;
use studioespresso\exporter\events\RegisterExportableFieldTypes;
use studioespresso\exporter\fields\MultiOptionsFieldParser;

Event::on(
FieldTypeHelper::class,
FieldTypeHelper::EVENT_REGISTER_EXPORTABLE_FIELD_TYPES,
function(RegisterExportableFieldTypes $event) {
$parsers = $event->fieldTypes;

$event->fieldTypes[MultiOptionsFieldParser::class] = array_merge($parsers[MultiOptionsFieldParser::class], [
\verbb\formie\fields\formfields\Checkboxes::class, // @phpstan-ignore-line
]);
}
);
````

## Included Parsers
- PlainTextParser
- RelationParser
- DateTimeParser
- TimeParser
- OptionsFieldParser
- MultiOptionsFieldParser
- MoneyFieldParser

There all live under the `studioespresso\exporter\fields\` namespace.
6 changes: 6 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ description: Documentation for Element Exporter, a plugin for Craft CMS.
---
# Plugin settings

## Sidebar Label
How the plugin will be called in the sidebar of the control panel

## HTML Email template
Path the template that will be used to send to export (as an attachment) to the provided email-address.


1 change: 1 addition & 0 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ function(RegisterExportableFieldTypes $event) {
\verbb\formie\fields\formfields\Radio::class, // @phpstan-ignore-line
\verbb\formie\fields\formfields\Dropdown::class, // @phpstan-ignore-line
]);

$event->fieldTypes[MultiOptionsFieldParser::class] = array_merge($parsers[MultiOptionsFieldParser::class], [
\verbb\formie\fields\formfields\Checkboxes::class, // @phpstan-ignore-line
]);
Expand Down
18 changes: 14 additions & 4 deletions src/helpers/FieldTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class FieldTypeHelper
{
public const EVENT_REGISTER_EXPORTABLE_FIELD_TYPES = 'registerExportableFieldTypes';

public const SUPPORTED_PARSERS = [
PlainTextParser::class => [],
RelationFieldParser::class => [],
DateTimeParser::class => [],
TimeParser::class => [],
OptionsFieldParser::class => [],
MultiOptionsFieldParser::class => [],
MoneyFieldParser::class => []
];

public const SUPPORTED_FIELD_TYPES = [
RelationFieldParser::class => [
Entries::class,
Expand Down Expand Up @@ -88,22 +98,22 @@ public function getAvailableFieldTypes(): array
}

$event = new RegisterExportableFieldTypes([
'fieldTypes' => self::SUPPORTED_FIELD_TYPES,
'fieldTypes' => self::SUPPORTED_PARSERS,
]);

Event::trigger(self::class, self::EVENT_REGISTER_EXPORTABLE_FIELD_TYPES, $event);

self::$_supportedFieldTypes = array_merge(
self::$_supportedFieldTypes = array_merge_recursive(
$event->fieldTypes,
self::SUPPORTED_FIELD_TYPES,
$event->fieldTypes
);

return self::$_supportedFieldTypes;
}

public function isFieldSupported(FieldInterface $field)
{
$item = array_filter(self::$_supportedFieldTypes, function($fields) use ($field) {
$item = array_filter(self::$_supportedFieldTypes, function ($fields) use ($field) {
foreach ($fields as $f) {
if ($f === get_class($field)) {
return true;
Expand Down

0 comments on commit 5ca460a

Please sign in to comment.