function template_preprocess_datatables_view in DataTables 7
Same name and namespace in other branches
- 6 datatables.module \template_preprocess_datatables_view()
- 7.2 datatables.module \template_preprocess_datatables_view()
Display a view as a DataTable style.
File
- ./
datatables.module, line 244 - Provides integration of the jQuery DataTables plugin
Code
function template_preprocess_datatables_view(&$vars) {
// Run views table preprocess function to handle putting rows in columns,
// classes, etc.
template_preprocess_views_view_table($vars);
// No need to add anything if there are no rows.
if (!$vars['rows']) {
return;
}
$view = $vars['view'];
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$fields =& $view->field;
$columns = $handler
->sanitize_columns($options['columns'], $fields);
$position = 0;
foreach ($columns as $field => $column) {
$column_options = NULL;
if (isset($options['hidden_columns'][$field])) {
switch ($options['hidden_columns'][$field]) {
case 'expandable':
$datatable_options['bExpandable'] = TRUE;
// ... and fall through, since expandable columns are also hidden.
case 'hidden':
// Hidden or expandable columns get the bVisible init option set to
// false.
$column_options['bVisible'] = FALSE;
}
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
// Overrides clicksort head defined in
// template_preprocess_views_view_table().
$vars['header'][$field] = check_plain(!empty($fields[$field]) ? $fields[$field]
->label() : '');
if (empty($options['info'][$field]['sortable']) || !$fields[$field]
->click_sortable()) {
$column_options['bSortable'] = FALSE;
}
else {
// Attempt to autodetect the type of field in order to handle sorting
// correctly.
if (drupal_strlen($fields[$field]->last_render) != drupal_strlen(strip_tags($fields[$field]->last_render))) {
$column_options['sType'] = 'html';
}
elseif (is_numeric($fields[$field]->last_render)) {
$column_options['sType'] = 'numeric';
}
elseif ($fields[$field] instanceof views_handler_field_date) {
$column_options['sType'] = 'date';
}
$column_options['bSortable'] = TRUE;
}
$datatable_options['aoColumns'][] = $column_options;
$datatable_options['aoColumnHeaders'][] = $vars['header'][$field];
}
// Set default sort order.
$datatable_options['aaSorting'] = array();
if ($options['default'] == $field) {
$datatable_options['aaSorting'] = array(
array(
$position,
$options['info'][$field]['default_sort_order'],
),
);
}
$position++;
}
// Enable table info display, if necessary.
$datatable_options['bInfo'] = $options['elements']['table_info'];
$datatable_options['bFilter'] = $options['elements']['search_box'];
$datatable_options['bStateSave'] = $options['elements']['save_state'];
$datatable_options['bLengthChange'] = $options['pages']['length_change'];
$datatable_options['iDisplayLength'] = (int) $options['pages']['display_length'];
// Enable ThemeRoller support, if necessary.
if ($options['layout']['themeroller']) {
$datatable_options['bJQueryUI'] = TRUE;
}
// Pass the sDOM parameter, if one is specified.
if ($options['layout']['sdom']) {
$datatable_options['sDom'] = $options['layout']['sdom'];
}
$datatable_options['bAutoWidth'] = $options['layout']['autowidth'];
// Enable full_numbers pagination if selected.
switch ($options['pages']['pagination_style']) {
case 'full_numbers':
$datatable_options['sPaginationType'] = 'full_numbers';
break;
case 'no_pagination':
$datatable_options['bPaginate'] = FALSE;
break;
default:
// Do nothing. No parameters need to be sent for the default (two-button)
// style.
break;
}
// Enable TableTools plugin support, if necessary.
if (!empty($options['elements']['table_tools'])) {
$datatable_options['oTableTools'] = array(
'sSwfPath' => base_path() . _datatables_get_path() . '/extras/TableTools/media/swf/copy_csv_xls_pdf.swf',
);
// If a custom sDom is passed, assume that "T" is added, otherwise add it
// manually.
if (!$options['layout']['sdom']) {
$datatable_options['sDom'] = 'T<"clear">lfrtip';
}
drupal_add_library('datatables', 'datatables-tabletools');
}
$datatable_options['oLanguage'] = array(
'sEmptyTable' => t('No data available in table'),
'sInfo' => t('Start !_START_ to !_END_ of !_TOTAL_ entries', array(
'!_START_' => '_START_',
'!_END_' => '_END_',
'!_TOTAL_' => '_TOTAL_',
)),
'sInfoEmpty' => t('Showing 0 to 0 of 0 entries'),
'sInfoFiltered' => t('(filtered from !_MAX_ total entries)', array(
'!_MAX_' => '_MAX_',
)),
'sInfoPostFix' => '',
'sProcessing' => t('Processing...'),
'sLengthMenu' => t('Show !_MENU_ entries', array(
'!_MENU_' => '_MENU_',
)),
'sLoadingRecords' => t('Loading...'),
'sZeroRecords' => t('No matching records found'),
'sSearch' => t('Search'),
'oPaginate' => array(
'sFirst' => t('First'),
'sPrevious' => t('Previous'),
'sNext' => t('Next'),
'sLast' => t('Last'),
),
'oAria' => array(
'sSortAscending' => t(': activate to sort column ascending'),
'sSortDescending' => t(': activate to sort column descending'),
),
);
$vars['id'] = _datatables_get_id();
$vars['classes_array'][] = 'display';
drupal_add_library('datatables', 'datatables');
drupal_add_js(array(
'datatables' => array(
'#' . $vars['id'] => $datatable_options,
),
), array(
'type' => 'setting',
'scope' => JS_DEFAULT,
));
}