Skip to content

Instantly share code, notes, and snippets.

@stevedev
Last active December 16, 2015 19:19
Show Gist options
  • Save stevedev/5484430 to your computer and use it in GitHub Desktop.
Save stevedev/5484430 to your computer and use it in GitHub Desktop.
<?php
Config::inst()->update( 'UserDefinedForm', 'extensions', array('UserDefinedFormExtension') );
(function($){
$.entwine('ss', function($) {
$('.ss-gridfield .action.userform-export-csv').entwine({
onclick: function(e){
var self = this, btn = this.closest(':button'), grid = this.getGridField();
var data = '';
// Add current button
data += '&' + encodeURIComponent(btn.attr('name')) + '=' + encodeURIComponent(btn.val());
// Include any GET parameters from the current URL, as the view state might depend on it.
// For example, a list prefiltered through external search criteria might be passed to GridField.
if(window.location.search) data = window.location.search.replace(/^\?/, '') + '&' + data;
window.location.href = $.path.makeUrlAbsolute(grid.data('url') + '?' + data, $('base').attr('href'));
return false;
}
});
});
}(jQuery));
<?php
class UserDefinedFormExtension extends DataExtension {
public function getCMSFields() {
$fields = parent::getCMSFields();
$this->extend( 'updateCMSFields', $fields );
return $fields;
}
public function updateCMSFields( FieldList $fields ) {
Requirements::javascript('mysite/javascript/MFUserForm.js');
$reports = $fields->fieldByName('Root.Submissions.Reports');
$config = $reports->getConfig();
// remove problematic export button
$config->removeComponentsByType( 'GridFieldExportButton' );
// remove print button. Because it sucks too.
$config->removeComponentsByType( 'GridFieldPrintButton' );
// add our own!
$config->addComponent( $export = new UserFormGridFieldExportButton() );
}
}
<?php
/**
* @package framework
* @subpackage gridfield
*/
/**
* Adds an "Export list" button to the bottom of a GridField.
*/
class UserFormGridFieldExportButton extends GridFieldExportButton {
/**
* Place the export button in a <p> tag below the field
*/
public function getHTMLFragments($gridField) {
$button = new GridField_FormAction(
$gridField,
'export',
_t('TableListField.CSVEXPORT', 'Export to CSV'),
'export',
null
);
$button->setAttribute('data-icon', 'download-csv');
$button->addExtraClass('userform-export-csv');
return array(
$this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>',
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment