2017-02-05 95 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

相關問題