FriendsOfCake/cakephp-csvview

Extending the CsvView class and RequestHandler

larsgw opened this issue · 2 comments

I am using CsvView, but I have to pre-process the data before serialization. Given the amount of code involved, I thought I'd put it in a separate view class rather than using _extract. However, when I point CSV in the viewClassMap to my custom view class, the value gets overwritten by this plugin.

EventManager::instance()->on('Controller.initialize', function (Event $event) {
$controller = $event->getSubject();
if ($controller->components()->has('RequestHandler')) {
$controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv');
}
});

I can make a PR to check whether the config is set before overriding it, would that be reasonable for the project? Something like this.

if ($controller->components()->has('RequestHandler') &&
    $controller->RequestHandler->getConfig('viewClassMap.csv') === null) { 
    $controller->RequestHandler->setConfig('viewClassMap.csv', 'CsvView.Csv'); 
}
ADmad commented

You can simply skip loading the bootstrap by using 'bootstrap' => false option when loading the plugin.

Ah, that works. Thank you!