While using the views_json.module to generate a feed, I noticed that the URLs contained in various fields (especially Body fields) had relative URLs. It's rather important for all URLs in my feed to be absolute because once someone has pulled the feed data, the content should link back to my site.

Below is code in my custom module that takes care of this. It would be trivial to add this as an option to the views style form for each plugin. What do you think?

/**
 * @file
 * Modifications to views_datasource rendering.
 */

/**
 * Implements hook_views_pre_render().
 */
function MYMODULE_views_pre_render(&$view) {
  global $base_url;

  $datasource_plugins = array(
    'views_json',
    'views_rdf',
    'views_xhtml',
    'views_xml',
  );

  if (isset($view->plugin_name) && in_array($view->plugin_name, $datasource_plugins)) {
    if (!empty($view->result)) {
      // Process each View result.
      foreach ($view->result as $row => $result) {
        // Only process the entity fields defined by the View.
        foreach ($view->field as $field_name => $field) {
          // Make all URLs absolute.
          if (!empty($view->result[$row]->{'field_' . $field_name}[0]['rendered']['#markup'])) {
            $val = $view->result[$row]->{'field_' . $field_name}[0]['rendered']['#markup'];
            $val = preg_replace('#((?:href|src)=")(/[^/])#i', '$1' . $base_url . '$2', $val);
            $view->result[$row]->{'field_' . $field_name}[0]['rendered']['#markup'] = $val;
          }
        }
      }
    }
  }
}

Comments

hargobind created an issue.