2017-02-05 2 views
0

モジュールを使用してビューのカスタムフィールドを作成しました。ここでより視覚的に表現するために、私はそれを単純化しました:カスタムフィールドは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()の主な目的は、ビューにデータベーステーブルを記述することです。 '#global' => array()を使用して実際にデータベースに存在しないフィールドを表示しましたが、その特定のフィールドがSQLクエリの一部ではなかったため、単純に並べ替えることはできません。 views_handler_my_custom_field->render()メソッドで得られた乱数の値さえ、すべてのソートが既に行われた瞬間に、ビューが生成されてSQLクエリを実行した後に生成されました。

+0

お時間をいただきありがとうございます!私は今、より良い論理を理解しています。残念ながら、この方法はうまくいかないので、私は「大きな写真」のアプローチを変えなければなりません。私が作成した実際のカスタムフィールドは、(コンテキストフィルタの場合と同様に)ログインユーザーに応じて動的です。問題は、それらの間のOR機能を使用して複数のコンテキストフィルタを必要とし、ユーザーが再生するために公開されることです。プランBを見つける時間... – Matt

関連する問題