I want to add new field in a existing view programmatically.
I am trying to use hook_views_pre_render().
function registration_export_views_pre_render(&$view) {
if ($view->name=='registrations_export') {
foreach($view->result as $r => $result) {
//dpm($result->registration_id);
$result->registration_id = 1;
}
}
}
If i add the "registration id" field to view page then i can see registration id field in my output. But i dont want to add in view field add page. I want to field add dynamically.
As per suggestion i have tried this
function registration_export_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'registrations_export') {
views_db_object::add_item($view->current_display, 'field',
'field_data_field_first_name_1', 'field_first_name_1',
$options = array(
$handler->display->display_options['fields']['field_first_name_1']['id'] = 'field_first_name_1',
$handler->display->display_options['fields']['field_first_name_1']['table'] = 'field_data_field_first_name_1',
$handler->display->display_options['fields']['field_first_name_1']['field'] = 'field_first_name_1',
), $id = NULL);
}
}
View export
$view = new view();
$view->name = 'registrations_export';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'registration';
$view->human_name = 'registrations_export';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'List of Event Registrations';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['grouping'] = array(
0 => array(
'field' => 'title',
'rendered' => 1,
'rendered_strip' => 1,
),
);
$handler->display->display_options['style_options']['columns'] = array(
'registration_id' => 'registration_id',
'nid' => 'nid',
'title' => 'title',
'field_first_name_1' => 'field_first_name_1',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
'registration_id' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'nid' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'title' => array(
'sortable' => 1,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'field_first_name_1' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
);
/* Relationship: Registration: Registration to Node */
$handler->display->display_options['relationships']['registration_related_node']['id'] = 'registration_related_node';
$handler->display->display_options['relationships']['registration_related_node']['table'] = 'registration';
$handler->display->display_options['relationships']['registration_related_node']['field'] = 'registration_related_node';
/* Field: Field: Salutation */
$handler->display->display_options['fields']['field_salutation_new']['id'] = 'field_salutation_new';
$handler->display->display_options['fields']['field_salutation_new']['table'] = 'field_data_field_salutation_new';
$handler->display->display_options['fields']['field_salutation_new']['field'] = 'field_salutation_new';
$handler->display->display_options['fields']['field_salutation_new']['exclude'] = TRUE;
/* Field: Field: State */
$handler->display->display_options['fields']['field_state']['id'] = 'field_state';
$handler->display->display_options['fields']['field_state']['table'] = 'field_data_field_state';
$handler->display->display_options['fields']['field_state']['field'] = 'field_state';
$handler->display->display_options['fields']['field_state']['exclude'] = TRUE;
/* Field: Field: Payment Type */
$handler->display->display_options['fields']['field_payment_type']['id'] = 'field_payment_type';
$handler->display->display_options['fields']['field_payment_type']['table'] = 'field_data_field_payment_type';
$handler->display->display_options['fields']['field_payment_type']['field'] = 'field_payment_type';
/* Field: Global: PHP */
$handler->display->display_options['fields']['php']['id'] = 'php';
$handler->display->display_options['fields']['php']['table'] = 'views';
$handler->display->display_options['fields']['php']['field'] = 'php';
$handler->display->display_options['fields']['php']['use_php_setup'] = 0;
$handler->display->display_options['fields']['php']['php_output'] = '<?php
//echo \'<pre>\';
//print_r($view->result[0]->_field_data[\'registration_id\'][\'entity\']);
//print_r($data);
//print_r($row->title);
?>';
$handler->display->display_options['fields']['php']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php']['php_click_sortable'] = '';
/* Filter criterion: Content: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'node';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['relationship'] = 'registration_related_node';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'Event Name';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'title';
$handler->display->display_options['filters']['title']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
4 => 0,
5 => 0,
7 => 0,
9 => 0,
10 => 0,
);
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'registrations-list';
/* Display: Data export */
$handler = $view->new_display('views_data_export', 'Data export', 'views_data_export_1');
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '0';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'views_data_export_csv';
$handler->display->display_options['style_options']['provide_file'] = 1;
$handler->display->display_options['style_options']['filename'] = '%timestamp-dd - %timestamp-mmm %view.csv';
$handler->display->display_options['style_options']['parent_sort'] = 0;
$handler->display->display_options['style_options']['quote'] = 1;
$handler->display->display_options['style_options']['trim'] = 0;
$handler->display->display_options['style_options']['replace_newlines'] = 0;
$handler->display->display_options['style_options']['header'] = 1;
$handler->display->display_options['style_options']['keep_html'] = 0;
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Content: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'node';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['relationship'] = 'registration_related_node';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'Event Name';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'title';
$handler->display->display_options['filters']['title']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
4 => 0,
5 => 0,
7 => 0,
9 => 0,
10 => 0,
);
$handler->display->display_options['path'] = 'registrations-list-export-csv';
$handler->display->display_options['displays'] = array(
'page' => 'page',
'default' => 0,
);
/* Display: Data export 2 */
$handler = $view->new_display('views_data_export', 'Data export 2', 'views_data_export_2');
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['style_plugin'] = 'views_data_export_xls';
$handler->display->display_options['style_options']['provide_file'] = 1;
$handler->display->display_options['style_options']['parent_sort'] = 0;
$handler->display->display_options['path'] = 'registrations-list-export-xls';
$handler->display->display_options['displays'] = array(
'page' => 'page',
'default' => 0,
);
Thank you very much.