How can I create a view, where all profile fields are as columns of a table view, without having to add each profile field indivudally? Is this possible?
2 Answers
I think it would work if you create a view that displays users as rendered entities, not fields.
You can then tweak the way user entities are rendered and show all fields.
-
Yes, that would work. I have forgotten to mention that I want to display the fields as a table in views.– StéphaneCommented May 13, 2015 at 12:28
I could solve this, with a custom module:
<?php
/**
* Implements hook_views_data().
*/
function profile2_all_fields_views_data_alter(&$data) {
$data['profile']['profile_all_fields'] = array(
'title' => t('All profile fields'),
'help' => t('When rendered, this field will be replaced by all the fields on the profile. No configuration is required.'),
'field' => array(
'title' => 'All profile fields',
'handler' => 'views_handler_field_custom',
),
);
}
function profile2_all_fields_views_pre_view(&$view, &$display_id, &$args)
{
// Replace the static field with dynamic fields.
// dsm($view->display_handler);
// dsm($view->display_handler->default_display, '$$view->display_handler->default_display');
if ($view->display_handler->options['fields']) {
$fields = &$view->display_handler->options['fields'];
} else {
$fields = &$view->display_handler->default_display->options['fields'];
}
foreach ($fields as $field_name => &$field) {
if ($field['field'] == 'profile_all_fields') {
$instances = field_info_instances('profile2', 'main');
dsm($fields, '$fields');
// Each field is added as a Views field.
foreach ($instances as $key => $instance) {
$newfield['id'] = $instance['field_name'];
$newfield['table'] = _field_sql_storage_tablename($instance);
$newfield['field'] = $instance['field_name'];
$fields[$key] = $newfield;
}
unset($fields[$field['id']]);
dsm($fields, '$fields');
}
}
}