0

I'm using services module to get data from database, among those data there is an image.

All fields are loaded except the image, the uri seems to be wrong.

here's the postman response :

  "writer": "writer1", <==== ok
  "picture": "public://couverture_harry_0.jpg",<=== not ok(wrong path)
  "review": null, <====== ok
  "date": "2010-02-05",<===== ok 

I've configured drupal to store on sites/default/files

the picture is there but the real path is :

drupal/sites/default/files/couverture_harry_0.jpg

(if I enter this path manually on postman it shows me the picture) But by default the path services put is wrong.

How to "configure" services or drupal to get the good path ?

EDIT This is my custom resource to retrieve a book (livre in french) :

function my_module_get_livre($entity_id) {
// check uid && entityID

$result = db_query("SELECT `field_title_value` as titre, `field_prix_value` as prix, `field_auteur_value` as auteur, `uri` as couverture, `field_resum__value` as resumé
    , `field_date_de_parution_value` as parution, `denomination_ed` as editeur,`field_nombre_de_pages_value` as pages FROM {field_data_field_titre_en_arabe} as t_ar
    inner join {field_data_field_prix} as pr on t_ar.entity_id = pr.entity_id
    inner join {field_data_field_auteur} as aut_ar on t_ar.entity_id = aut_ar.entity_id
    left join {field_data_field_photo_de_couverture} as ph_couv on t_ar.entity_id = ph_couv.entity_id
    left join {file_managed} as file on ph_couv.field_photo_de_couverture_fid = file.fid
    left join {field_data_field_resum_} as resu on t_ar.entity_id = resu.entity_id
    left join {field_data_field_date_de_parution} as d_paru on t_ar.entity_id= d_paru.entity_id
    left join {field_data_field_edit} as edi on t_ar.entity_id = edi.entity_id
    left join {editeur} as ed on edi.field_edit_target_id = ed.uid
    left join {field_data_field_nombre_de_pages} as pages on t_ar.entity_id = pages.entity_id
    WHERE t_ar.entity_id = $entity_id ")->fetchAll();

return array('code'=>'0', 'response'=>$result);
   }


 function my_module_livre_retrieve ($entity_id){
      return my_module_get_livre($entity_id);
    }

I've also tested this (it works) :

$fid ='2'; <=== i put it manually(but it would be set by select query
$file = file_load($fid);
$realURI = $file->uri_full = file_create_url($file->uri);
var_dump($realURI);<==== returns the real url (localhost/drupal/sites/...

I want to 'alter' or just put the $realURI value to every book(livre) in his field : "photo de couverture" (which is the pitcture i want)

Thank you

  • 4
    I'm not sure you'd call it 'wrong', unexpected maybe if you're not used to seeing Drupal's public URI scheme. That's how it's stored in the database, so services just proxies it straight through to the client. You can either implement the translation to the proper URL (simple string replace) in your client app, or alter it in Drupal (e.g. drupal.stackexchange.com/questions/63780/…) – Clive Aug 12 '15 at 15:59
  • Thank you Clive, after making some research I think i've to use realpath(), but how to use it just before returning a select query result ? Maybe i should edit my question and add the retrieve services function ? – Mitch Aug 13 '15 at 9:21
  • That would be useful @Mitch, if this is a custom resource you're writing it should be easy to change the results – Clive Aug 13 '15 at 9:24
  • Clive i've just edited my question please check it – Mitch Aug 13 '15 at 9:33
  • 2
    Easy then: loop over the results, replace the uri field in each with file_create_url(...uri...) and that should be it – Clive Aug 13 '15 at 9:35
0

Yes, this is an annoyance, no doubt. Drupal stores the uri using a public:// streamwrapper. Thankfully, someone has written the code to resolve that into a full url that gets attached to the file object for you whenever one's loaded: https://www.drupal.org/project/full_url_file_info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.