简体   繁体   English

如何在Drupal 7视图中使引用列可排序

[英]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..). 我在Drupal 7中有一个视图,它显示用户信息,例如(名称,地址,状态等)。 I have one column in this (Table)view as "Published event". 我在此(Table)视图中有一列为“已发布事件”。 Basically events are created by users do I want to make this column sortable. 基本上,事件是由用户创建的,我是否想使此列可排序。 I have attached image for more reference. 我已附上图片以供更多参考。

截图

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 上面的代码在“ tcd_reporting.views_default.inc”文件中,如果我输入“ sortable => 1”,它仍然不提供排序

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,

); );

[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 . 为了使click排序在指定列上可行,其字段处理程序必须定义两个函数: click_sortableclick_sort First one just need to return true , while second need to properly implements sorting on view. 第一个只需要返回true ,而第二个需要正确地实现对视图的排序。 For example see handler: [views_module_path]/handlers/views_handler_field.inc . 例如,请参阅处理程序: [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 ). 看来您的“已发布事件”列已定义了处理程序,该处理程序没有click_sortableclick_sort函数(或click_sortable仅返回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. 查找定义视图源的位置(这取决于您如何告知视图,如果我了解“用户信息”之类的信息(可能在hook_entity_info函数或hook_views_data函数中),请检查为您的“已发布事件”分配了哪些处理程序字段并进行更改。

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. 我建议您尝试创建hook_views_data_alter函数,并使用dpm()进行启动。 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 编辑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 示例:我创建了hook_views_data_alter函数来查看视图数据的内容

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

You might need to clear cache to see dpm of *_alter hooks. 您可能需要清除缓存以查看* _alter钩子的dpm。 Inside dpm'ed array I found "users" for generic example, and its field name looks like this: 在dpm'ed数组中,我找到了通用示例的“用户”,其字段名称如下所示:

DPM

I suggest you to try alter your field with click_sortable = TRUE and see what happens. 建议您尝试使用click_sortable = TRUE更改您的字段,然后看看会发生什么。 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. 如果这没有帮助,请提供有关您的字段,如何创建该字段,在hook_views_data_alter中的hook_views_data_alter以及定义的处理程序的更多信息。

Edit 2 编辑2

Ok, so you have your views exported to code into views_default file. 好的,因此您已将视图导出到代码中,并放入views_default文件中。 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 ). 但这仅允许您将您从数据库创建的视图导出到代码,因此,这基本上反映了您在视图Web编辑器中所做的工作(例如,页面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 ). 您需要做的是更改其中一个字段的行为,使其变为可排序的(在处理程序类中添加click_sortableclick_sort函数), 或者将该字段的处理程序更改为带有排序选项的一个(将field处理程序更改为另一个,例如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 1 ,检查dpm,然后尝试更改$ data数组以找到解决方案。

Edit 3 编辑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). 创建新视图时,请选择基于该特定视图的集合(最简单的示例-它可能是MySQL表,并且视图将使用SQL查询从中检索数据)。 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. User是数据库表user ,它是您在创建新视图时选择的源。 创建视图

  2. Field - eg. 领域-例如 mail which is database column mail , this fields you add to your view. mail ,即数据库列mail ,您将其添加到视图中。

  3. Field handler - eg. 现场服务员-例如 views_handler_field_numeric , this is class name of handler to use by specified field views_handler_field_numeric ,这是要由指定字段使用的处理程序的类名

Now, if you don't created your own handler then your field "Published event" have one of generic views handler. 现在,如果您没有创建自己的处理程序,则字段“ Published event”将具有通用视图处理程序之一。 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. 这就是为什么我建议添加功能click_sortableclick_sort建议不正确。 Instead you should change handler responsible for field "Published event". 相反,您应该更改负责字段“ 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. 如果某种程度上不可能,那么我能想到的唯一方法就是hook_views_data_alter 请参阅文档以获取更多信息和示例。 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. 我想您应该尝试将字段的处理程序重新定义为通用数字处理程序views_handler_field_numeric因为它应该具有完整的排序功能,或者尝试将click_sortable属性添加到字段数组中,如您在我的帖子的第一张图片中所见,但我无法提供您经过充分测试的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM