SanjPrajapati
SanjPrajapati

Reputation: 33

How to make referenced column sortable in drupal 7 view

I have one view in Drupal 7, which displays user information like (Name, address, Status, etc..). I have one column in this (Table)view as "Published event". Basically events are created by users do I want to make this column sortable. I have attached image for more reference.

Screenshot

I tried with applying relationship but no success.

table settings

my handler code is like below :

 $handler->display->display_options['sorts']['event_count_published']   ['id'] = 'event_count_published';

$handler->display->display_options['sorts']['event_count_published']   ['table'] = 'search_api_index_user_search_index';
 $handler->display->display_options['sorts']['event_count_published']   ['field'] = 'event_count_published';
$handler->display->display_options['sorts']['event_count_published']   ['order'] = 'DESC';

 'mail' => array(
  'sortable' => 1,
  'default_sort_order' => 'asc',
  'align' => '',
  'separator' => '',
  'empty_column' => 0,
),
'event_count_published' => array(
  'align' => '',
  'separator' => '',
  'empty_column' => 0,
  'sortable' => 1,
),

above code is in "tcd_reporting.views_default.inc" file, if I put 'sortable => 1', it still does not provide sorting

field is created by below code:

 $properties['event_count_published'] = array(
'label' => t('Published Events'),
'description' => t('Number of published events authored by user.'),
'type' => 'integer',
'getter callback' => 'tcd_event_content_type_count_published_get',
'computed' => TRUE,
'entity views field' => TRUE,

);

Upvotes: 0

Views: 610

Answers (1)

Jacek Rosłan
Jacek Rosłan

Reputation: 333

[Introduction] Which function is responsible for 'click sort' in views?

Click sort -this checkbox from your second screen- in views table settings is function which is enabled only for fields which have properly defined handlers. As you may know each field in views have few handlers (for displaying, filtering, sorting). And for click sort to be possible on specified column its field handler must have two functions defined: click_sortable and click_sort. First one just need to return true, while second need to properly implements sorting on view. For example see handler: [views_module_path]/handlers/views_handler_field.inc.

Your case:

It seems that your column "Published event" have defined handler which does not have click_sortable and click_sort functions (or click_sortable simply returns false).

Possible fix:

Find place where you defined your view source (it depends on how you informed views about it, if I understand its something like "User info" - maybe in hook_entity_info function or hook_views_data function), check what handler is assigned to your "Published event" field and change it.

It's hard to tell where you need to look as it depends on your implementation.

I suggest you to try create hook_views_data_alter function and dpm() it for start. Later you can alter it like that:

mymodule_views_data_alter(&$data) {
  $data['some_view_info']['published_event']['field']['handler'] = 'views_handler_field_numeric';
}

Edit 1

First could you tell where this code is? Is it inside handler class, or maybe some views hook? Views gives you a lot of flexibility but this make them hard to understand, and I'm not sure what exactly you achieve and how.

Assuming your field works properly you can try to simply enable click sort.

Example: I created hook_views_data_alter function to see content of views data

function mymodule_views_data_alter(&$data) {
  dpm($data,'d');
}

You might need to clear cache to see dpm of *_alter hooks. Inside dpm'ed array I found "users" for generic example, and its field name looks like this:

dpm

I suggest you to try alter your field with click_sortable = TRUE and see what happens. If this wont help please provide more information about your field, how you created it, how it looks in hook_views_data_alter and which handlers it has defined.

Edit 2

Ok, so you have your views exported to code into views_default file. But this only allows you to export view you created from database to code, so it is basically a reflection of what you done in views web editor (eg. page yourwebsite.com/admin/structure/views/view/your_view_name/edit). What you need to do is to change behavior of one of your fields so it became sortable (add click_sortable and click_sort functions in handler class) or change handler of this field to one with sorting option (change field handler to other one like views_handler_field_numeric). If you don't have experience in creating handlers and this is one of generic handlers i suggest you to go back to my Edit 1, examine your dpm, and try to alter $data array to find solution.

Edit 3

Little explanation to prevent confusion. When creating new view you select collection on which this particular view base on (simpliest example - it may be MySQL table, and view will use SQL queries to retrieve data from it). By digging down we have:

  1. Collection - eg. User which is database table user, it is what you select as source when creating new view. create view

  2. Field - eg. mail which is database column mail, this fields you add to your view.

  3. Field handler - eg. views_handler_field_numeric, this is class name of handler to use by specified field

Now, if you don't created your own handler then your field "Published event" have one of generic views handler. You shouldn't ever change code of contributed modules - especially so widely used as views handlers. That's why my suggestion to add functions click_sortable and click_sort is incorrect. Instead you should change handler responsible for field "Published event".

Best way is to define proper handler in place where you define your field "Published event". If it's somehow impossible the only way I can think of is hook_views_data_alter see docs for more info and examples. I suppose you should try to redefine handler of your field to generic numeric handler views_handler_field_numeric as it should have full sorting functionallity, or try to add click_sortable property to field array as you can see in first image of my post, but I can't provide you fully tested example.

Upvotes: 1

Related Questions