Skip to content

Instantly share code, notes, and snippets.

@davereid
Created December 5, 2012 20:06
Show Gist options
  • Save davereid/4219021 to your computer and use it in GitHub Desktop.
Save davereid/4219021 to your computer and use it in GitHub Desktop.
Adding sorts for long text fields in Views
<?php
/**
* Implements hook_field_views_data_alter().
*
* Views disables sorting on any field columns that have a schema type of 'text'
* even though it's valid to sort on those fields. Force those field columns to
* have a sort available in Views.
*
* CAUTION: Sorting on these fields may cause performance issues with queries.
*/
function custom_field_views_data_alter(&$data, $field, $module) {
foreach ($field['columns'] as $column_name => $column) {
if ($column['type'] == 'text') {
// field_views_data() only processes fields using sql storage.
$storage_column_name = _field_sql_storage_columnname($field['field_name'], $column_name);
foreach ($data as $table => &$field_data) {
if (empty($field_data[$storage_column_name]['sort'])) {
$field_data[$storage_column_name]['sort'] = array(
'field' => $storage_column_name,
'table' => $table,
'handler' => 'views_handler_sort',
'additional fields' => $field_data[$storage_column_name]['filter']['additional fields'],
'field_name' => $field['field_name'],
);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment