function services_views_execute_view in Services Views 7
Execute a view and return results.
1 call to services_views_execute_view()
- views_plugin_display_services::preview in includes/
views/ views_plugin_display_services.inc - Fully render the display.
1 string reference to 'services_views_execute_view'
- services_views_services_resources in ./
services_views.module - Implements hook_services_resources().
File
- ./
services_views.resource.inc, line 129 - Callbacks for services module resource hooks.
Code
function services_views_execute_view($view_info, $view = NULL, $display_id = NULL) {
if (!empty($view_info)) {
$view_name = $view_info['view_name'];
$display_id = $view_info['display_id'];
$args = $view_info['args'];
$view = views_get_view($view_name);
$view
->set_arguments($args);
$view
->execute_display($display_id, $args);
}
// Avoid translation of the field labels.
$view->localization_plugin = views_get_plugin('localization', 'none');
// Execute a view.
$output = $view
->render($display_id);
// Add support for Services Raw formatter.
foreach ($view->result as $index => $row) {
foreach ($view->field as $field_key => $field) {
$f_key = 'field_' . $field_key;
$entity_property = FALSE;
if (!isset($row->{$f_key})) {
$f_key = $field->field_alias;
$entity_property = TRUE;
}
if (!isset($row->{$f_key})) {
continue;
}
$target_key = $field_key;
if (isset($field->options['hide_empty']) && $field->options['hide_empty'] == TRUE && empty($output[$index]->{$target_key})) {
unset($output[$index]->{$target_key});
continue;
}
if (isset($field->options['hide_alter_empty']) && $field->options['hide_alter_empty'] == TRUE && empty($output[$index]->{$target_key})) {
unset($output[$index]->{$target_key});
continue;
}
// Skip this field?
if (isset($field->options['exclude']) && $field->options['exclude'] == TRUE) {
continue;
}
if (isset($field->options['label']) && !empty($field->options['label'])) {
$target_key = $field->options['label'];
}
// Move key if we have an entity property and the target_key was changed
// via label.
if ($entity_property) {
unset($output[$index]->{$f_key});
$field->view->row_index = $index;
// @todo: Do not render twice; use view->execute_display and
// consolidate functions.
$output[$index]->{$target_key} = $field
->theme($row);
continue;
}
// Create helper variables.
$output[$index]->{$target_key} = array();
if ($field->field_info['cardinality'] == 1) {
$output[$index]->{$target_key} = '';
}
$obj =& $output[$index]->{$target_key};
$format = $field->options['type'] == 'services' ? 'raw' : 'rendered';
foreach ($row->{$f_key} as $idx => $res) {
if ($idx == 0) {
$obj = [];
}
if (isset($res[$format])) {
$data = $res[$format];
// If this is raw formatter.
if ($format == 'raw') {
if (is_array($data)) {
foreach ($data as $key => $val) {
if ($field->options['settings']['skip_safe'] && strpos($key, 'safe_') === 0) {
unset($data[$key]);
}
if ($field->options['settings']['skip_empty_values'] && empty($val)) {
unset($data[$key]);
}
}
}
$obj[$idx] = $data;
}
elseif (isset($data['#access']) && $data['#access'] == TRUE) {
// Check for a field collection.
if (isset($data['entity']['field_collection_item'])) {
foreach ($data['entity']['field_collection_item'] as $fcid => $entity) {
// If the field collection is using the services view mode,
// add each child separately.
if ($entity['#view_mode'] == 'services') {
$instances = field_info_instances($entity['#entity_type'], $entity['#bundle']);
foreach (element_children($entity, TRUE) as $child) {
$instance = $instances[$child];
$element_key = $instance['display']['services']['settings']['data_element_key'];
$element_key = $element_key ? $element_key : $instance['label'];
// Get the formatted values for the field and add them to
// the data array, using any preset element keys.
foreach (element_children($entity[$child]) as $field_item) {
$fi =& $entity[$child][$field_item];
if (is_array($fi) && count($fi) == 1 && isset($fi['value'])) {
$obj[$idx][$element_key] = $fi['value'];
}
else {
$obj[$idx][$element_key] = $fi;
}
}
}
// Allow modules to alter the data before it is sent.
drupal_alter('services_field_collection_data', $obj[$idx], $entity, $f_key);
}
else {
$obj[$idx] = render($data);
}
}
}
else {
$obj[$idx] = render($data);
}
}
else {
$obj[$idx] = array(
'error' => t('Access denied or format unknown on field.'),
);
}
}
// Check html_strip property.
if ($field->options['alter']['strip_tags'] == 1) {
// Strip the tags which aren't allowed.
$obj[$idx] = strip_tags($obj[$idx], $field->options['alter']['preserve_tags']);
}
// Check cardinality of the field.
if ($field->field_info['cardinality'] == 1 && is_array($obj) && count($obj) == 1) {
$obj = $obj[$idx];
}
}
}
}
// Add support Header/Footer elements of view
$header = services_views_element_retrieve($view->header);
$footer = services_views_element_retrieve($view->footer);
empty($header) ?: ($output['header'] = $header);
empty($footer) ?: ($output['footer'] = $footer);
drupal_alter('services_views_execute_view', $output, $view);
$view
->destroy();
return $output;
}