在Views handlers中字段没有初始化

在Views Sort中field没有字段初始化, 在Filter handler中。如果你想使用$this->query->fields 这是不可能的。因为现在他还是一个空数组。并没有初始化, 这个fields 是你add_field添加的字段,如果你要使用你就必须初始化

解决方案:

//这样就可以初始化你的field
$this->view->_build('field');

options_form算是最开始运行的,所以如果你要在这里面使用$this->query->fields,你还需要初始化handlers和query

$this->view->init_handlers();      
$this->view->init_query();
$this->view->_build('field');

Drupal Views API

Views 3 API: http://api.drupal.org/api/views

Views2 手册 : http://views.doc.logrus.com/

http://api.drupal.org/api/views/views.api.php/7

about Views Handlers:
http://api.drupal.org/api/views/views.api.php/group/views_handlers/7

Views 分为两个很大的部分:
1. handlers
2. plugin

Handlers :

在Views, handlers 主要是 query的生成
http://api.drupal.org/api/views/views.api.php/group/views_handlers/7

plugin :

在Views中, plugin主要是用来做显示方面的工作
http://api.drupal.org/api/views/views.api.php/group/views_plugins/7

Object: http://api.drupal.org/api/views/includes%21base.inc/class/views_object/7
基本功能: http://drupal.org/node/59157

http://drupal.org/node/322506

获取定义的Views数据:

views_fetch_data()

获取在添加Views时的那些列表,也就是base table:

views_fetch_base_tables()

后台Views操作获取 field/sort/filter/argument/relationship/area 字段列表
文件: views/includes/admin.inc

//生成表单
views_ui_add_item_form
//根据参数获取field
views_fetch_fields

Views 操作

views_get_enabled_views($view) //判断一个View是否是启动状态
views_view_is_disabled($view)  //判断一个View是否是禁用状态
views_get_all_views()          //获取所有views
views_get_enabled_views        //获取所有启用的views
views_get_disabled_views       //获取所有已禁用的views

Views 常用函数

views_get_all_views()        //显示所有views
views_get_view('archive')    //获取指定的views
views_block_view($name)      //获取views中指定的block,viewsname-blockMachineName
views_page($view,$page_name) //显示指定views的指定page

Drupal Views handler

http://api.drupal.org/api/views/views.api.php/group/views_handlers/7
http://www.ericschaefer.org/blog/2011/01/09/custom-field-handlers-for-views-2-drupal
http://treehouseagency.com/blog/neil-hastings/2010/01/19/views-handler-easy-way

Views Handler Query执行流程:

handler->construct()
handler->set_definition()
handler->init()
handler->pre_query()
handler->query()

比如当前在myuser模块
创建目录
includes/views/handlers

修改.info ,添加引用文件
myuser.info

;views handler
files[] = includes/views/handlers/views_handler_field_user_nodes_count.inc
myuser.module 添加代码. 让views从这个目录去自动载入 MODULENAME.views.inc文件

在myuser中定义hook

/**
 * Implements hook_views_api().
 */
function myuser_views_api() {
  return array(
      'api' => '3.0',
      'path' => drupal_get_path('module', 'myuser') . '/includes/views',
  );
}

在includes/views/handlers创建文件, 这一块功能是对user组添加一个字段提供选择
创建文件: myuser.views.inc
当面你也可以登陆hook_views_data(&$data)来完成

function haier_user_views_data_alter(&$data){
  $data['users']['count_nodes'] = array(
    'title' => t('Count nodes total'),
    'help' => t('Display the total count of the nodes authored by user'),
    'field' => array(
      'title' => t('Node Total'),
      'help' => t('Display the total count of the nodes authored by user'),
      'handler' => 'views_handler_field_user_nodes_count',
      'no group by' => TRUE,
    ),
  );
}

创建views_handler_field_user_nodes_count.inc文件 创建类继承views_handler_field_numeric

class views_handler_field_user_nodes_count extends views_handler_field_numeric {

    function option_definition() {
    $options = parent::option_definition();

    $options['entity_type'] = array('default' => 'node', 'translatable' => TRUE);

    return $options;
  }

  function options_form(&$form, &$form_state) {

    $form['entity_type'] = array(
      '#type' => 'select',
      '#title' => t('Entity Type'),
      '#options' => array(
        'node' => t('Node'),
        'comment' => t('Comment'),
        'votingapi_vote'=>t('Vote'),
        'field_data_field_idea_status'=>t('Adoption')
      ),
      '#default_value' => $this->options['entity_type'],
      '#description' => t('For which entity type authored the count should be calculated.'),
      '#size' => 2,
    );

    parent::options_form($form, $form_state);
  }
    //TODO: Extend this module to be a user authored entity count
  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();

    //Init the count for all users in a static variable

    $entity_type = $this->options['entity_type'];

    $query = db_select('users', 'u');
    $query->fields('u', array('uid'));
    $query->condition('u.status', 0, '<>');

    $nodes_count_array = &drupal_static($this->options['entity_type'].'_count_array', array());
    $nodes_count_array = $query->execute()->fetchAllKeyed();

  }

  function render($values) {
    $nodes_count_array = drupal_static($this->options['entity_type'].'_count_array', array());
    return (isset($nodes_count_array[$values->uid]) && is_numeric($nodes_count_array[$values->uid])) ? $nodes_count_array[$values->uid] : 0;
  }

}

Views Hook

http://api.drupal.org/api/views/views.api.php/7 API
http://drupal.org/documentation/modules/views DOcument

hook_views_data
hook_views_data_alter(&$data) 把你自已模块中的数据字段加进views,过滤表结构
hook_views_plugins
hook_views_plugins_alter
hook_views_api 用来告诉views从哪个目录去寻找MODULENAME.views.inc的文件,注册一个View API信息
hook_views_default_views
hook_views_default_views_alter
hook_views_pre_view
hook_views_pre_build
hook_views_post_build 查询现在完全建成,但是还没有运行db_rewrite_sql
hook_views_pre_execute
hook_views_post_execute 此Hook是查询处理后. 到pre_render()前提供
hook_views_pre_render
hook_views_post_render 输出所有渲染的数据

hook_views_query_alter(&$view, &$query) 修改views生成的sql

hook_views_preview_info_alter
hook_views_ui_display_top_links_alter
hook_views_ajax_data_alter 它允许你修改views的ajax请求
hook_views_invalidate_cache