作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com
在field_collection_views.views.inc,我们实现了hook_views_data这个钩子,为['field_collection_item']追加了几个新的字段,追加这些字段的目的是,为了在Views里面拼凑出来用来编辑、删除、添加的链接。
<?php
/**
* @file
* Provide extras views data for field_collection.module.
*/
/**
* Implements hook_views_data()
*/
function field_collection_views_views_data() {
// hostEntityId
$data['field_collection_item']['host_entity_id'] = array(
'title' => t('Host Entity ID'),
'help' => t('The ID of the Host Entity.'),
'field' => array(
'handler' => 'field_collection_views_handler_field_host_entity_id',
),
);
$data['field_collection_item']['host_entity_path'] = array(
'title' => t('Host Entity Path'),
'help' => t('The Path of the Host Entity.'),
'field' => array(
'handler' => 'field_collection_views_handler_field_host_entity_path',
),
);
$data['field_collection_item']['host_entity_type'] = array(
'title' => t('Host Entity Type'),
'help' => t('The Type of the Host Entity.'),
'field' => array(
'handler' => 'field_collection_views_handler_field_host_entity_type',
),
);
$data['field_collection_item']['field_path'] = array(
'title' => t('Field path'),
'help' => t('The base path of the field-collection field.'),
'field' => array(
'handler' => 'field_collection_views_handler_field_field_path',
),
);
return $data;
}
这里,有一些可以改进的地方,比如这里,我们可以使用hook_views_data_alter,而不是使用hook_views_data。为什么呢?因为我们这里没有定义自己的表,只是为已有的表添加字段而已。
另外的一个改进,可能就是,直接使用:
$data['field_collection_item']['edit_field_collection_item ']
$data['field_collection_item']['delete_field_collection_item ']
而不是去拼凑我们的字段了。