Warning: You are browsing the documentation from version 4 to 10 of Pimcore. Please visit https://pimcore.com/docs/platform/ for the latest versions of Pimcore.
Version:

File exporter - Events

There are a couple of events which you can use to modify the export.

  • pimcore.dataobject.postCsvItemExport
  • Pimcore\Bundle\DataHubFileExportBundle\Event\IsValidExportItemEvent
  • Pimcore\Bundle\DataHubFileExportBundle\Event\FilterExistingDataEvent
  • Pimcore\Bundle\DataHubFileExportBundle\Event\PreWriteDataEvent
  • Pimcore\Bundle\DataHubFileExportBundle\Event\RelevantItemListEvent
  • Pimcore\Bundle\DataHubFileExportBundle\Event\FilenameEvent

Sample implementation

Sample configuration for EventListener
    AppBundle\EventListener\DataHubFileExportListener:
        tags:
            - { name: kernel.event_listener, event: pimcore.dataobject.postCsvItemExport, method: postCsvItemExport }
            - { name: kernel.event_listener, event: Pimcore\Bundle\DataHubFileExportBundle\Event\IsValidExportItemEvent, method: isValidExportItem }
            - { name: kernel.event_listener, event: Pimcore\Bundle\DataHubFileExportBundle\Event\FilterExistingDataEvent, method: filterExistingData }
            - { name: kernel.event_listener, event: Pimcore\Bundle\DataHubFileExportBundle\Event\PreWriteDataEvent, method: preWriteData }
            - { name: kernel.event_listener, event: Pimcore\Bundle\DataHubFileExportBundle\Event\RelevantItemListEvent, method: relevantItemList }
            - { name: kernel.event_listener, event: Pimcore\Bundle\DataHubFileExportBundle\Event\FilenameEvent, method: modifyFilename }
The appropriate EventListener would look like this
<?php

/**
 * Pimcore
 *
 * This source file is available under two different licenses:
 * - GNU General Public License version 3 (GPLv3)
 * - Pimcore Commercial License (PCL)
 * Full copyright and license information is available in
 * LICENSE.md which is distributed with this source code.
 *
 *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
 *  @license    http://www.pimcore.org/license     GPLv3 and PCL
 */

namespace AppBundle\EventListener;

use Pimcore\Bundle\DataHubFileExportBundle\Event\FilenameEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Event\FilterExistingDataEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Event\IsValidExportItemEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Event\PreWriteDataEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Event\RelevantItemListEvent;
use Pimcore\DataObject\Import\Resolver\Filename;
use Symfony\Component\EventDispatcher\GenericEvent;
use \Pimcore\Bundle\DataHubFileExportBundle\Exporter\AbstractExporter;

class DataHubFileExportListener
{
    public static function postCsvItemExport(\Pimcore\Event\Model\DataObjectEvent $event ){
    
            $object = $event->getObject();
            if($object instanceof \Pimcore\Model\DataObject\Car){
                $context = $event->getArgument('context');
                if($context['source'] == 'pimcore_data_hub_file_export'){
                    $objectData = $event->getArgument('objectData');
                    /**
                     * @var $exporter \Pimcore\Bundle\DataHubFileExportBundle\Exporter\AbstractExporter
                     */
                    if($context['configId']){ //from preview mode
                        $config = \Pimcore\Bundle\DataHubBundle\Configuration::getByName($context['configId']);
                        $exporter = \Pimcore\Bundle\DataHubFileExportBundle\Helper::getExporterService($config);
                    }else{
                        $exporter = $context['exporter']; //from export
                    }
    
    
                    $dataHubConfig = $exporter->getDataHubConfiguration();
                    $objectData['id'] .= 'modified by ev';
    
    
                    if($dataHubConfig->getName() == 'test-json'){
                        $objectData['name'] = $objectData['name'] . ' - Modified by event';
                        if($objectData['manufacturer']){
                            $objectData['manufacturer'] = 'xxx - ' . $objectData['manufacturer'];
                        }
    
                        $event->setArgument('objectData',$objectData);
                    }
                }elseif($context['source'] == 'pimcore-export'){
                    //pimcore grid export
                    $objectData = $event->getArgument('objectData');
                    $objectData['fullpath'] .= '- Modified....';
    
                    $event->setArgument('objectData',$objectData);
                }
    
            }
        }

    public static function modifyFilename(FilenameEvent $event){

        /**
         * @var $exporter AbstractExporter
         */
        $exporter = $event->getExporter();
        $filename = $event->getFilename();

        $filename = strtoupper($filename);
        $event->setFilename($filename);
    }

    public static function isValidExportItem(IsValidExportItemEvent $event){

        /**
         * @var $exporter AbstractExporter
         */
        $exporter = $event->getExporter();
        $object = $event->getObject();

        if($object->getId() == 82){
            $event->setIsValid(false);
        }
     }

     public static function relevantItemList(RelevantItemListEvent $event){
         /**
          * @var $exporter AbstractExporter
          * @var $list \Pimcore\Model\DataObject\Listing
          */
         $exporter = $event->getExporter();
         $list = $event->getList();
         $condition = $list->getCondition();
         $list->setCondition($condition .= ' AND o_id=123');

     }

     public static function preWriteData(PreWriteDataEvent $event){
         /**
          * @var $exporter AbstractExporter
          */
         $exporter = $event->getExporter();
         $data = $event->getData();

         foreach($data as $i => $e){
             $e['name'] = 'hello - ' . $i;
             $data[$i] = $e;
         }

         $event->setData($data);
     }

     public static function filterExistingData(FilterExistingDataEvent $event){
         /**
          * @var $exporter \Pimcore\Bundle\DataHubFileExportBundle\Exporter\AbstractExporter
          */
         $exporter = $event->getExporter();
         $data = $event->getData();
         foreach($data as $i => $e){
             $o = \Pimcore\Model\DataObject\Concrete::getById($e['id']);
             if(!$exporter->isValidExportItem($o)){
                 unset($data[$i]);
             }
         }
         $event->setData($data);
     }
}