You are here

public function views_plugin_display_services::execute in Services Views 7

When used externally, this is how a view gets run and returns data in the format required.

The base class cannot be executed.

Overrides views_plugin_display::execute

1 call to views_plugin_display_services::execute()
views_plugin_display_services::render in includes/views/views_plugin_display_services.inc
Render this display.

File

includes/views/views_plugin_display_services.inc, line 36
Contains the page display plugin.

Class

views_plugin_display_services
The plugin that handles a Services callback.

Code

public function execute() {
  if (!$this
    ->access()) {
    global $user;
    module_load_include('inc', 'services', 'includes/services.runtime');
    return services_error(t('Access denied for user @user', array(
      '@user' => isset($user->name) ? $user->name : 'anonymous',
    )), 401);
  }

  // Prior to this being called, the $view should already be set to this
  // display, and arguments should be set on the view.
  $this->view
    ->build();

  // Execute a view.
  $this->view
    ->execute();
  foreach ($this->view->field as $field) {
    $field
      ->pre_render($this->view->result);
  }

  // Render the fields.
  if (isset($this->view->style_plugin)) {
    $this->view->style_plugin
      ->render_fields($this->view->result);
  }

  // Map fields.
  $result = array();
  foreach ($this->view->result as $row_id => $item) {
    $new_item = new stdClass();
    foreach ($this->view->field as $field_name => $field) {

      // Skip this field?
      if (isset($field->options['exclude']) && $field->options['exclude'] == TRUE) {
        continue;
      }

      // Make sure new keys are alphanumerical, underscore and dash.
      $custom_key = !empty($field->options['label']) && preg_match('/^[0-9A-Za-z_-]*$/', $field->options['label']) ? $field->options['label'] : FALSE;
      if (isset($item->{$field->field_alias})) {
        $key = $custom_key ? $custom_key : $field->field_alias;
        $new_item->{$key} = $item->{$field->field_alias};
      }
      elseif (!empty($this->view->style_plugin->rendered_fields[$row_id][$field_name])) {
        $key = $custom_key ? $custom_key : $field_name;
        $new_item->{$key} = $this->view->style_plugin->rendered_fields[$row_id][$field_name];
      }
    }
    if (!empty($new_item)) {
      $result[] = $new_item;
    }
  }
  return $result;
}