Accessing variables inside multiple arrays in a custom node

Events happening in the community are now at Drupal community events on www.drupal.org.
thepocketgeek's picture

I am creating a customer node.tpl.php file for a content type. I found a great video podcast on the basics of creating one at http://mustardseedmedia.com/podcast. They have some outstanding tutorials that have really helped me.

I did run into one small problem that they didn't cover in the video. Using the print_r($node) function, I was able to get an output array of the variables that are used to create the content node. I get the part where if I want to display the path in my custom node, all I have to do is something like:

<a href="<?php print $node->path?>">Click Me</a>

But what if I want to access something that is inside of a multiple array like an image file? I have posted an excerpt from the print_r($node) output.

[path] => articles/number-6-truck
    [field_main_image] => Array
        (
            [0] => Array
                (
                    [fid] => 38
                    [list] => 1
                    [data] => Array
                        (
                            [description] =>
                            [alt] =>
                            [title] =>
                        )

                    [uid] => 1
                    [filename] => matthews-mobile-number-6-truck.jpg
                    [filepath] => sites/default/files/matthews-mobile-number-6-truck_0.jpg <--- **** THIS IS WHAT I WANT TO BE ABLE TO ACCESS ***
                    [filemime] => image/jpeg
                    [filesize] => 909754
                    [status] => 1
                    [timestamp] => 1281541418
                    [nid] => 133
                    [view] =>
                )

        )

So, how do I get to it?

I looked up multiple arrays on php.net but the suggestions they supplied there don't work in Drupal (or, equally likely, I didn't employ them properly).

Any ideas?

Comments

I believe it would be

gilcreque's picture

I believe it would be $node->field_main_image['0']['filepath']

Just so that I understand it

thepocketgeek's picture

Just so that I understand it correctly (you know, since I am going to want to pull more stuff from still more arrays)...

$node -> field_with_array_inside['number of the subarray']['field name you need']

Is that about right?

Here is a question. I got the

thepocketgeek's picture

Here is a question. I got the image to show up but it is giving me the raw format. I set up an imagecache setting for the content type to scale and crop the images that are loaded as part of the node but that is not the image that is showing up.

Is there a way to access them POST process? I was under the impression that when you created the node, it created an imagecache version of the image loaded and then called that one when building the node.

Is that how it works or is it "created" every time the node is built?

I thought imagecache created

bhosmer's picture

I thought imagecache created the image dynamically when it was requested.

That's what I thought too but

thepocketgeek's picture

That's what I thought too but when I called the image it gives me the full size one. I can't find a reference to it in the print_r($node) output, though, admittedly, I could be overlooking it.

The node maintains a

garethsprice's picture

The node maintains a relationship to the original copy of the image, then the ImageCache image is displayed in a separate step.

This is so that you can set up multiple ImageCache sizes and still have an original reference image to refer back to.

To render an imagecache version of the image, use:

print theme('imagecache', 'imagecache_preset_name', $path, $alt, $title, $attributes);

Orlando, FL Web Developer | http://www.garethsprice.com/

Am I using that inside of am

thepocketgeek's picture

Am I using that inside of am image tag (Only local images are allowed.) or am I just putting that inside of a div tag?

EDIT:
I tried it inside of a div tag and it sort of worked. The CSS setting I set up for the image cache worked but the image was missing. When I inspected the image the file type (i.e .jpg) was missing. Would that be an imagecache setting or an adjustment needed to the theme() function?

Not sure... can you paste the

garethsprice's picture

Not sure... can you paste the HTML output you get?

Orlando, FL Web Developer | http://www.garethsprice.com/

Here is the div where I

thepocketgeek's picture

Here is the div where I placed the theme() snippet:

<div>Image:<img src="http://matthewsbuses.com/sites/default/files/imagecache/index_news/articles/number-6-truck" alt="" title="Number 6 Truck"  class="imagecache imagecache-index_news" /></div>

I noticed something else...

thepocketgeek's picture

When I looked at the file name that was being fed to the HTML output, I noticed that it was wrong. Even if the .jpg file extension had been there, it would not have found the file. Where is the $path variable being pulled from?

print theme('imagecache', 'index_news', $path, $alt, $title, $attributes);

I found a blog post that helped shed some light on the function but I still don't know why the image path is incorrect.

Billy, Most of what you have

liberatr's picture

Billy, Most of what you have looks correct, except for $path. I think that is the path to the current node. Probably in the example you found, they were using $path as a placeholder, or in a different context. try:

<?php
print theme('imagecache', 'index_news', $node->field_main_image['0']['filepath'], $alt, $title, $attributes);
?>

You also may need to change all of the $alt $title and $attribtues to indexes of the field_main_image['0'] array as well, like
field_main_image['0']['data']['alt']

Florida

Group organizers

Group categories

Florida Topics

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: