Sorry to open up another ticket on a similar subject, but I've posed questions on stack overflow, the forums, and no one seems to know the answer to my questions. Basically I'm trying to alter the output of a field before it is delivered to my service. So far I have tried, field templates (which aren't used for services). as well as theme_preprocess_views_view_fields, which again, is never used because it the theme is never rendered. Is there a function to alter the output of the field before it goes to the services view ?

Comments

dippers’s picture

Do you actually mean before the 'services view' (before the view is built and rendered) or do you mean before the output is sent from the endpoint (after the view is rendered)? The latter can be done using:

function hook_services_request_postprocess_alter($controller, $args, &$result){}

and the modify the result array.

cdmo’s picture

Thanks, I didn't ask this question, but I did have the same one and that function helped me alter the json output of a service.

I wonder, is this hook (or any other Services Views hooks) documented anywhere?

version: 7.x-1.0-beta2

ygerasimov’s picture

Status: Active » Fixed

I have added basic documentation about hook_services_request_preprocess_alter() and hook_services_request_postprocess_alter() hooks to Services module.

cdmo’s picture

Thank you very much!

dereckd’s picture

This does seem to address the output of altering the view it's self, but I'm interested in altering the output of the field only. For instance, no matter how I attempt to format things, the user image is always output as a url in HTML .... I would like to strip the html before the services output.

dippers’s picture

The way I got around this was to write a custom field formatter to output raw urls. You could also do it using views-php of a display suite custom field but they will all require some php knowledge.

dippers’s picture

The following will give you a custom field formatter for images and cropped images.

function MYMODULE_field_formatter_info() {
  return array(
    'MYMODULE_raw_image_url' => array(
      'label' => t('Image Url'),
      'field types' => array('image', 'imagefield_crop'),
      'settings' => array('image_style' => ''),  
    ),
  );
}

function MYMODULE_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  
  switch ($display['type']) {
  case 'MYMODULE_raw_image_url':
    $image_styles = image_style_options(FALSE);
    $element['image_style'] = array(
      '#title' => t('Image style'),
      '#type' => 'select',
      '#default_value' => $settings['image_style'],
      '#empty_option' => t('None (original image)'),
      '#options' => $image_styles,
    );    
    break;
  }
  return $element;
}

function MYMODULE_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = '';
  
  switch ($display['type']) {
  case 'MYMODULE_raw_image_url':
    $image_styles = image_style_options(FALSE);
    // Unset possible 'No defined styles' option.
    unset($image_styles['']);
    // Styles could be lost because of enabled/disabled modules that defines
    // their styles in code.
    if (isset($image_styles[$settings['image_style']])) {
      $summary = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
    }
    else {
      $summary = t('Original image');
    }    
    break; 
  }
  return $summary;
}

function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $elements = array();
  switch ($display['type']) {
  case 'MYMODULE_raw_image_url':
    foreach ($items as $delta => $item) {
      $elements[$delta]['#markup'] = image_style_url($display['settings']['image_style'], $item['uri']);
    }
    break;
  }
  return $elements;
}
dereckd’s picture

That is exactly what I was looking for, for my use case I ended up using a very simple approach to the formatter

/**
 * Implements hook_field_formatter_view().
 */
function videofieldformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array(); // Initialize the var

    foreach ($items as $delta => $item) {
        //dd($item['uri']);
        $element[$delta] = array('#markup' => $settings['some_setting'] . file_create_url($item['uri']));
    }
    return $element;
}

I'm still a bit confused about the #markup portion of the array, but none the less it works exactly like I wanted

Thanks

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

DrCord’s picture

Issue summary: View changes

I was dealing with an issue with IP addresses not being converted from long to an actual IP, the hook_services_request_postprocess_alter hook helped!

Here's the code:

function admin_dashboard_services_request_postprocess_alter($controller, $args, &$result){
    //alter output of IP address from long int to formatted IP address
    //IP Manager Module in charge of IPs, stored as long, needs to be converted to be useful
    foreach($result as $k => $user){
        $long2ip = !empty($user->ip_address) ? long2ip($user->ip_address) : NULL;
        $user->ip_address = !empty($long2ip) ? $long2ip : $user->ip_address;
    }
}