After using views_get_view($view_name)
how do I get the names of the fields? The list of field names (arranged correctly) is going to be pushed onto an array following which will be all the results. I can see in the return value from views_get_view()
the field
array, but how do I get them as they would be displayed in a table, that is, correct capitalization, spaces instead of underscores, etc.?
1 Answer
There is an array of fields in the display object:
$view->display['YOURDISPLAYNAME']->display_options['fields']
for example if you want to retrieve list of field names of the view arbitrary display, you'd need to do something like this:
function get_views_field_names($view_name, $display = 'default') {
$view = views_get_view($view_name);
$result = array();
foreach($view->display[$display]->display_options['fields'] as $field){
$result[] = $field->label;
}
return $result;
}
and somewhere in the code
$fields = get_views_field_names('YOURVIEWNAME', 'YOURDISPLAYNAME');
-
Huge help, but I had to make a couple changes. I added
if !isset($view->display[$display]->display_options['fields'])){$display = 'default';}
in case the display is supplied but uses the defaults. Also,$field
is an array not an object so I used brackets.– htoipApr 28, 2012 at 11:10