2017-02-05 106 views
0

我已经使用模块为View创建了自定义字段。为了更好地在这里可视化,我简化了它:自定义域简单地生成1到10之间的随机数。自定义字段无法在视图中排序或过滤Drupal 7

我想“排序”这个随机数。

SQLSTATE [42S22]:列未找到:在“字段列表”

我奋力查找错误1054未知列“my_custom_field”不过我用浏览此功能时,收到以下错误在我的代码中。

感谢您在我的模块代码中提供的任何帮助!

这里是我的文件:

my_custom_module.info

name = My Custom Module 
description = Implement random number in views. 
core = 7.x 
files[] = includes/views_handler_my_custom_field.inc 

my_custom_module.module

<?php 
/** 
* Implements hook_views_api(). 
*/ 
function my_custom_module_views_api() { 
    return array(
    'api' => 3, 
); 
} 

my_custom_module.views.inc

<?php 
/** 
* Implements hook_views_data(). 
*/ 
function my_custom_module_views_data() { 
    $data['my_custom_module']['table']['group'] = t('My custom module'); 
    $data['my_custom_module']['table']['join'] = array(
    // Exist in all views. 
    '#global' => array(), 
); 

    $data['my_custom_module']['my_custom_field'] = array(
    'title' => t('My custom field'), 
    'help' => t('My custom field displays a random number.'), 
    'field' => array(
     'handler' => 'views_handler_my_custom_field', 
     'click sortable' => TRUE, 
    ), 
    'sort' => array(
     'handler' => 'views_handler_sort', 
    ), 
    'filter' => array(
     'handler' => 'views_handler_filter_numeric', 
    ), 
); 

    return $data; 
} 

views_handler_my_custom_field.inc

<?php 
/** 
* @file 
* Custom views handler definition. 
*/ 

/** 
* Custom handler class. 
* 
* @ingroup views_field_handlers 
*/ 
class views_handler_my_custom_field extends views_handler_field { 
    /** 
    * {@inheritdoc} 
    * 
    * Perform any database or cache data retrieval here. In this example there is 
    * none. 
    */ 
    function query() { 

    } 

    /** 
    * {@inheritdoc} 
    * 
    * Modify any end user views settings here. Debug $options to view the field 
    * settings you can change. 
    */ 
    function option_definition() { 
    $options = parent::option_definition(); 
    return $options; 
    } 

    /** 
    * {@inheritdoc} 
    * 
    * Make changes to the field settings form seen by the end user when adding 
    * your field. 
    */ 
    function options_form(&$form, &$form_state) { 
    parent::options_form($form, $form_state); 
    } 

    /** 
    * Render the random field. 
    */ 
    public function render($values) { 
    $random = rand(1, 10); 
    return $random; 
    } 
} 

回答

0

简短的回答:你无法排序没有相应的数据库字段中的视图。

稍微长一点的答案:hook_views_data()的主要目的是描述一个数据库表到Views。您确实使用'#global' => array()显示了数据库中并不存在的字段,但由于该特定字段不属于SQL查询的一部分,因此您无法对其进行排序。即使您在views_handler_my_custom_field->render()方法中获得的随机数的值是在视图生成并执行SQL查询之后生成的,此时所有排序都已经发生。

+0

感谢您花时间回答!我现在更好地理解逻辑。不幸的是,我不得不改变我的'更大的图片'的方法,因为这种方法不起作用。根据登录用户创建的实际自定义字段是动态的(与上下文筛选器一样)。问题是我需要多个上下文过滤器,使用它们之间的或功能,并暴露给用户使用。时间找到计划B ... – Matt

相关问题