Tags/topics: 
5
Answers
Vote up!
0
Vote down!

How to print an image field url

I have been looking for information on how to reference the url of the image in the product image field but all the information out there is suggesting views???

Although views and commerce integration is amazing, there are still some use cases (social media plugin for example) when a simple php call for the image url is needed.

In my case I have a commerce product with an image field and a product display with a product reference.
I need to be able to print the referenced product image url in the product display.

Any suggestions?

Asked by: ahimsauzi
on June 14, 2012

5 Answers

Vote up!
1
Vote down!

One way to get it would be:

<?php
 $image_url
= file_create_url($content['product:field_image']['#items'][0]['uri']);
?>
Andy Giles
Answer by: Andy Giles
Posted: Jun 15, 2012
Vote up!
1
Vote down!

I would say download and enable the devel module (http://drupal.org/project/devel), then in your theme, go to the node.tpl.php file (or better yet create a node--product-display.tpl.php). Then paste this at the bottom:

<?php
 dpm
($content);
?>

It will print an array in the message area of your site for admins. You can drill down through your product display elements here by clicking on the darker orange rows to expand them and see what lays within the array.

You should see a product:field_image row or something like that. Within that array, there is a path. It will probably be something similar to this:

<?php
 
print $content['product:field_image'][0]['#path']['path'];
?>
Answer by: Aaron Dudenhofer
Posted: Jun 14, 2012
Vote up!
0
Vote down!

Thanks dudenhofer!

I was using devel but for some reason could not dig deep to the image uri. I tried the #object array first which did not work then out of desperation looked at every array until I found the #items array and used:

<?php print $content['product:field_image']['#items']['0']['uri']; ?>

That returned public://field/image/image-name.jpg, not ideal at all (who use public:// anywhere on the web???).

I ended up using the following:

<?php print t('<a href="http://www.example.com/sites/default/files/field/image/'">http://www.example.com/sites/default/files/field/image/'</a>); ?><?php print $content['product:field_image']['#items']['0']['filename']; ?></div>

Seemed like a hack to me so if anyone has a better way, please please...

Answer by: ahimsauzi
Posted: Jun 15, 2012
Vote up!
0
Vote down!

Awesome thanks andyg5000! worked like a charm!

Answer by: ahimsauzi
Posted: Jun 15, 2012