Customize and Extend
There are multiple ways for extending the file export adapter.
It is possible to add additional file types and transmitters (see below) and there are a couple of events that allow you to further customize the export.
In addition to that, it is also possible to completely customize the whole exporter by providing a custom exporter service. This gives you full control over the whole export process and allows things like adding custom query conditions for the workspaces etc.
Additional Transmitters
Transmitters are responsible for delivering the file to its desired destination (local directory, remote location, etc.). If you want to add further transmitters just provide a PHP service class like this:
<?php
namespace App\DataHubFileExport\Exporter\Transmitter;
class MyCustomTransmitter extends \Pimcore\Bundle\DataHubFileExportBundle\Exporter\Transmitter\AbstractTransmitter {
public function execute()
{
$exporter = $this->getExporter();
$config = $exporter->getDataHubConfiguration()->getConfiguration();
$transmitterConfig = $config['deliveryDestination']['transmitter_myCustomTransmitter'];
// Process the transmitter configuration and push data to your destination
// $transmitterConfig contains the settings from the configuration dialog
}
}
... and add it as a Service with the tag pimcore.datahub.fileExport.exporter.transmitter
App\DataSyndicator\Exporter\Transmitter\MyCustomTransmitter:
shared: false
tags:
- {
name: "pimcore.datahub.fileExport.exporter.transmitter",
type: "myCustomTransmitter",
}
For the JavaScript part (custom configuration fields for your transmitter) you can provide a fieldset like follows.
If you registered the PHP Service with the type myCustomTransmitter, your id has to be transmitter_myCustomTransmitter
(type prefixed with transmitter_). Please make sure that the id naming convention is correct.
pimcoreDataHubFileExportBundlePlugin.transmitter.myCustomTransmitter =
function () {
let id = "transmitter_myCustomTransmitter";
return {
xtype: "fieldset",
title: t(
"plugin_pimcore_datahub_delivery_destination_transmitter_myCustomTransmitter"
),
width: 600,
itemId: id,
defaultType: "textfield",
hidden: false,
items: [
{
fieldLabel: t(
"plugin_pimcore_datahub_delivery_destination_directory"
),
name: id + ".directory",
width: 560,
value: this.getValue(
"deliveryDestination." + id + ".directory"
),
},
],
};
};
If you don't need further configuration, you can just define an empty function, so the transmitter shows up in the
Delivery type dropdown:
pimcoreDataHubFileExportBundlePlugin.transmitter.myCustomTransmitter =
function () {};
Please make sure with bundle priorities, that your Bundle is loaded after the DataHub File Export, otherwise you will get a Javascript error because the variable doesn't exist.
Additional Exporter Types
To add additional exporter types (besides CSV, XML and JSON) just have a look at the existing ones. You can create
custom exporter types by adding additional services that extend the Pimcore\Bundle\DataHubFileExportBundle\Exporter\Type\AbstractExporter
class and register them with the tag pimcore.datahub.fileExport.exporter.type.
App\Exporter\Type\JSON:
shared: false
tags:
- { name: "pimcore.datahub.fileExport.exporter.type", type: "MY_JSON" }
Customize built in Exporter Types/Transmitters
To customize the built in exporter/transmitter you can override the existing services. Please take a look at Service definition of the bundle on how they are defined.
If you want to override the XML exporter type, you could do it like this:
Create your service class e.g:
<?php
namespace App\DataSyndicator\Exporter\Type;
class XML extends \Pimcore\Bundle\DataHubFileExportBundle\Exporter\Type\XML
{
protected $itemKey = 'myCustomItemKey';
protected function writeData($data)
{
return parent::writeData($data);
}
protected function getExistingData(): array
{
return parent::getExistingData();
}
}
... and override the service definition in your services.yml with:
Pimcore\Bundle\DataHubFileExportBundle\Exporter\Type\XML:
class: App\DataSyndicator\Exporter\Type\XML
shared: false
tags:
- { name: "pimcore.datahub.fileExport.exporter.type", type: "XML" }
This would just change the XML node item key from item to myCustomItemKey.
Extending the JSON Exporter
An example of extending the JSON exporter to nest the data under a key, such that an object is top level instead of an array:
class JSONNested extends \Pimcore\Bundle\DataHubFileExportBundle\Exporter\Type\JSON
{
protected $itemKey = 'items';
protected function writeData($data)
{
return parent::writeData([$this->itemKey => $data]);
}
protected function extractData($data)
{
return $data[$this->itemKey];
}
}
which takes advantage of the protected method extractData.
Custom exporter services
The exporter service implements and controls the whole export process. The default bundle configuration for the file exporter looks like this. So you can override custom parts if you need it.
pimcore_data_hub_file_export:
defaultExporter: "pimcore.datahub.fileExport.exporter.file"
By default, the pimcore.datahub.fileExport.exporter.file service (see settings above) is used to execute the
export. You can change the exporter globally (settings above) or per export configuration.
To create a custom exporter service you have to register the class as a public service.
Extending the Studio Exporter
To customize data extraction while keeping the Studio GridService integration, extend StudioFile
instead of File:
<?php
namespace App\DataHubFileExport\Exporter;
use Pimcore\Bundle\DataHubFileExportBundle\Exporter\File;
use Pimcore\Model\DataObject;
class MyStudioFile extends File
{
public function getItemData(DataObject\Concrete $object): array|bool
{
$data = parent::getItemData($object);
if ($data === false) {
return false;
}
// Add a computed field
$data['Full Name'] = ($data['First Name'] ?? '') . ' ' . ($data['Last Name'] ?? '');
return $data;
}
}
Register it as a service:
app.datahub.my-studio-exporter:
class: App\DataHubFileExport\Exporter\MyStudioFile
public: true
shared: false
tags:
- { name: "pimcore.datahub.fileExport.exporter" }
Then set app.datahub.my-studio-exporter as the custom exporter service in the DataHub configuration,
or change the default exporter globally.
Change download date
This option allows you to control whether the filename reflects the download event or the file creation event.
pimcore_data_hub_file_export:
useDownloadDateForFileName: false
If useDownloadDateForFileName is set to true, the exported file's name will include the date and time when the file is downloaded. If set to false, the file name will use the date and time when the file was originally generated.
Sample
Definition
app.datahub.my-exporter:
class: App\DataSyndicator\Exporter\File
public: true
shared: false
tags:
- { name: "pimcore.datahub.fileExport.exporter" }
Please make sure it is public, not shared and tagged as in the sample above!
Implementation
<?php
namespace App\DataSyndicator\Exporter;
class File extends \Pimcore\Bundle\DataHubFileExportBundle\Exporter\File {
public function execute($data)
{
// Perform custom processing before calling the parent
return parent::execute($data);
}
}