主题稍有偏离,但是此答案显示了如何以编程方式执行此操作(例如,通过将其作为模块更新添加到MY_MODULE.install文件中)。
如果要在最后一个现有字段之前添加新字段,则需要做更多的工作。将其合并到$ view-> display ['default']-> display_options ['fields']数组的末尾。
function MY_MODULE_update_7101(){
// update the admin/content view, need to do it manually because it's
// set by admin_views module
$view_name = 'admin_views_node';
$view = views_get_view($view_name, TRUE);
// add the relationship
$view->display['default']->display_options['relationships']['uid_1']['id'] = 'uid_1';
$view->display['default']->display_options['relationships']['uid_1']['table'] = 'node_revision';
$view->display['default']->display_options['relationships']['uid_1']['field'] = 'uid';
$view->display['default']->display_options['relationships']['uid_1']['label'] = 'Revision User';
// new column settings
$new_column = array(
'name_1' => array(
'id' => 'name_1',
'table' => 'users',
'field' => 'name',
'relationship' => 'uid_1',
'label' => 'Updated By',
)
);
// need to use this because array_splice by itself resets 'name_1' key to '0'
// see http://php.net/manual/en/function.array-splice.php#56794
$temp_array = array_splice( $view->display['default']->display_options['fields'] , 0, 7);
$view->display['default']->display_options['fields'] = array_merge($temp_array , $new_column, $view->display['default']->display_options['fields']);
views_save_view($view);
}