When Configuring a Page display for a View, select "File: Path (Path)" as one of the fields (see attached screenshot Selected Fields).

Edit the "File: Path (Path)" field by selecting the "Display download path instead of file storage URI" option (see attached screenshot Download Path Instead Of File Storage URI).

In the preview of the Page, the download path is displayed instead of the file storage URI (see attached screenshot Views Preview).

When the Services display of the View is configured the same way, the file storage URI is still displayed instead of the download path (see attached screenshot Services Preview).

I am requesting that the Services display recognize the selection of the 'Download path instead of file storage URI' option and display the download path.

Thanks!

Comments

Vito Andolini’s picture

OK, I figured out how to do this. I am a complete newbie to both Drupal and PHP, so I don't know how to create a patch to the module or anything. But the code to do it is below. Basically I updated the execute method with the lines in between the // START CHANGE HERE and // END CHANGE HERE lines. All this does is return a download URL for a file instead of the file storage URI *if* the user selected that option.

My php skills are 0, so maybe you guys know a better way to do this. If this could make it into the next release, that would be great. Thanks so much for providing this great module.

  function execute() {

    // 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();

    // Map fields.
    $result = array();
    foreach ($this->view->result as $item) {
      $new_item = new stdClass();
      foreach ($this->view->field as $field) {
        if (isset($item->{$field->field_alias})) {
          // Make sure new keys are alphanumerical, underscore and dash.
          if (!empty($field->options['label']) && preg_match('/^[0-9a-z_-]*$/', $field->options['label'])) {
            $new_item->{$field->options['label']} = $item->{$field->field_alias};
          }
          else {
            $new_item->{$field->field_alias} = $item->{$field->field_alias};
          }
          // START CHANGE HERE
          if (strcasecmp($field->field_alias, "file_managed_uri") == 0) {
          	$data = $item->{$field->field_alias};
    		if (!empty($field->options['file_download_path']) && $data !== NULL && $data !== '') {
          	  $new_item->{$field->field_alias} = file_create_url($new_item->{$field->field_alias});
    	  	}
    	  } 
          // END CHANGE HERE
        }
      }
      if (!empty($new_item)) {
        $result[] = $new_item;
      }
    }

    return $result;
  }
Vito Andolini’s picture

This code is actually a little cleaner:

          if (strcasecmp($field->field_alias, "file_managed_uri") == 0) {
          	$data = $new_item->{$field->field_alias};
    		if (!empty($field->options['file_download_path']) && $data !== NULL && $data !== '') {
          	  $new_item->{$field->field_alias} = file_create_url($data);
    	  	}
    	  } 
ygerasimov’s picture

Status: Active » Fixed

Thank you for the solution. I think proper way to accomplish this is to use views_php module and build downloadable url using some custom php code.

focal55’s picture

To follow ygerasimov, using a views_php field you can grab the uri value from the $data object and pass it to file_create_url to print.

Note that you need to create a relationship to the file with "File Usage: File".

Status: Fixed » Closed (fixed)

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

admataz’s picture

Status: Closed (fixed) » Active

I would vote for the suggested functionality in Vito's code in #1790956-1: Services View Always Shows File Storage URI Instead of Download Path for File Path Field to be added - it doesn't make sense for a Service to generate File Storage URIs - there is no way for external systems to consume these URIs, and this is the purpose of this module. The views_php solution may work, but seems like an extra hack for something that should be part of the way services_views generates file paths.

msakurada’s picture

This does work on version 7.x-1.0-beta2 w/ views 7.x-3.6 and services 7.x-3.3 downloaded today. you do have to create a File Usage: File relationship but it outputs a real URL Path:

{
node_title: "<a href="/d7dev/node/3">Accumsan Blandit Facilisis Validus</a>",
post date: "1364402110",
nid: "3",
img_path: "http://localhost/d7dev/sites/default/files/field/image/imagefield_YqO8GF.jpg"
},
ygerasimov’s picture

Status: Active » Fixed

@msakurada thank you for this solution. It is way more elegant than using php code!

Status: Fixed » Closed (fixed)

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

dsrikanth’s picture

@msakurada: Did you add Views PHP or anything else? I use the same versions mentioned by you but still getting public://filename.jpg. Yes, I have the File Usage: File relationship...

Raphael Dürst’s picture

@dsrikanth: I just had the same problem. In the views preview, it shows the file storage URI (public://filename.jpg), but if you actually look at the real output of the service, you get the download path.

Zheko’s picture

Issue summary: View changes

Here's my solution for this issue. Maybe help for someone.

    foreach ($this->view->result as $item) {
      $new_item = new stdClass();
      foreach ($this->view->field as $field) {
		if (isset($item->{$field->field_alias})) {
			
          // Make sure new keys are alphanumerical, underscore and dash.
           if (!empty($field->options['label']) && preg_match('/^[0-9a-z_-]*$/', $field->options['label'])) {
			$field_label = $field->options['label'];
          }
          else {
			$field_label = $field->field_alias;
          }
           $new_item->{$field_label} = $item->{$field->field_alias};

		  if (strcasecmp($field->field_alias, "file_managed_file_usage_uri") == 0) {
          	$data = $new_item->{$field_label};
    		if (!empty($field->options['file_download_path']) && $data !== NULL && $data !== '') {
          	  $new_item->{$field_label} = file_create_url($data);
    	  	}
    	  } 
        }
      }
      if (!empty($new_item)) {
        $result[] = $new_item;
      }
    }
dsrikanth’s picture

I can confirm that the File Usage relationship and adding the File Path field with "Display download path instead of file storage URI " enabled works.

Services Version: 7.x-3.12
Services Views Version: 7.x-1.1
Views Version: 7.x-3.11