Hi,

i'm using image_attach in a content-type of mine.

I am also using the ContentTemplate (aka Contemplate) module to theme CCK teasers and body, but seems that if i edit the default ContentTemplate, the image i attached disappears (as does disappear any other non-CCK extra content! Already filed a feature request on Contemplate project page about this.)

So the question is:
Which php snippet shall one put in his template to output a picture uploaded with image_attach?

I tryed different ways, but didn't succeeded in this, could any good soul help please?

Comments

garg’s picture

Hello. Were you able to find a solution for this? Thanks

david lesieur’s picture

Title: How to output image attached with img_attach? » How to output image attached with image_attach?

Try this:

  $node = node_load($nid);
  print theme('image_attach_body', $node);
Patrick Nelson’s picture

Thanks David - works a treat and it was just what I was looking for!

Thanks also kiteatlas for having already asked the question I was going to ask!

Patrick Nelson’s picture

Actually, one question... how do you test to see if there is actually an image attached? The code displays the following error if there isn't actually an image attached:

Warning: Invalid argument supplied for foreach() in /var/www/modules/node.module on line 359

Warning: implode(): Bad arguments. in /var/www/modules/node.module on line 363

Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: node_load SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.moderate, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in /var/www/includes/database.mysql.inc on line 120
david lesieur’s picture

Good question! Then try this:

  if ($iid) {
    $node = node_load($nid);
    print theme('image_attach_body', $node);
  }
Patrick Nelson’s picture

Fantastic - works a treat. Thanks again, David.

Regards

Patrick

dodorama’s picture

what if I just want to output the img tag and not the themed image ?

dodorama’s picture

I had the answer by myself.
If you just want the img tag with link use:

<?php
  if ($iid) {
    $node = node_load($nid);
    $image = node_load($node->iid);
    print l(image_display($image, 'thumbnail'), "node/$node->nid", array(), NULL, NULL, FALSE, TRUE);
  }
  ?>

If you just need to print the url to the image to tag it yourself use:

<?php
  if ($iid) {
    $node = node_load($nid);
    $image = node_load($node->iid);
    print file_create_path($image->images['thumbnail']);
  }
  ?>

It works for me but take this with care, since my php skills are poor.

alexmay’s picture

Thanks to all!
I have used code below, but it was only links to node, wich contain attached image.
I have modifyed your code, and now I get link to image page!!!

$node = node_load($nid);
    $image = node_load($node->iid);
    print l(image_display($image, 'thumbnail'), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);   

alexmay’s picture

I don't find the way, how attach more then 1 image to CCK-generated node, but I have enabled file (not image) attachment and I can now attach 1 main image (via image_upload module) and other images/files via file_attach.
Now, my final way to display 1 main image (thumbnail+link) and links (without previews :( ) to other images.
Here is:

$node = node_load($nid);
    $image = node_load($node->iid);
// here we insert thumbnail with link to image node
    print l(image_display($image, 'thumbnail'), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);   

// here we show all attachments to this CCK node
 print theme('upload_attachments', $files); 
Luneh’s picture

Hi there,

I hope that you will be able to help a Drupal newbie struggling to get a nice site.

What I need to do with image_attach is to display the image separately from the other content of the node. I would like the image to be in another region defined in the template.

I tried to create a block with this code:

<?php
  if ($iid) {
    $node = node_load($nid);
    print theme('image_attach_body', $node);
  }
?>

but nothing appeared on my page. When checking the page source, I can see that nothing that could be part of the block is mentioned either.

Anybody knows what I could do?

Many thanks in advance.

Caroline

Luneh’s picture

Hi,

More about my problem.

I am totally newbie with php but it seems like ut does not find any image attached to my node (I can confirm there is one, it even appears above it at the moment).

When trying to use

<?php
  $node = node_load($nid);
  print theme('image_attach_body', $node);
?>

in order to get rid of the "if" statement, I get this error:

    * warning: implode(): Bad arguments. in /home/hyboria/www/modules/node.module on line 378.
    * user warning: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.moderate, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in /home/hyboria/www/includes/database.mysql.inc on line 121.
    * warning: implode(): Bad arguments. in /home/hyboria/www/modules/node.module on line 378.
    * user warning: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 query: SELECT n.nid, n.vid, n.type, n.status, n.created, n.changed, n.comment, n.promote, n.moderate, n.sticky, r.timestamp AS revision_timestamp, r.title, r.body, r.teaser, r.log, r.format, u.uid, u.name, u.picture, u.data FROM node n INNER JOIN users u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE in /home/hyboria/www/includes/database.mysql.inc on line 121.

Can somebody please help me find what to do?

Thanks.

Luneh’s picture

Again, made some tests.

Here is something which works in a block:

<?php
    $node = node_load(arg(1));
    $image = node_load($node->iid);
    print l(image_display($image, 'image_node'), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
  ?>

It returns the image attached to the node we are on.

The problem is that I still get a SQL error on nodes which do not have any image attached. Which means that I need the "if" statement but I can't find how to build it.

if ($iid) { is not working as $iid is not defined. I guess that I need to define $iid before anything else but how to do it? I have found this in the image_attach.module code which could be a start but I do not exactly know how to use it:

    case 'load':
      $iid = db_result(db_query("SELECT iid FROM {image_attach} WHERE nid=%d", $node->nid));
      return array('iid' => $iid);
david lesieur’s picture

Try this (untested):

  $node = node_load(arg(1));
  if ($node->iid) {
    $image = node_load($node->iid);
    print l(image_display($image, 'image_node'), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
  }
Luneh’s picture

Thanks, that did the trick ;)

drewish’s picture

Status: Active » Fixed

then mark it as fixed ;)

The.Clue’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev
Status: Fixed » Active

snippets here work as a charme....but i still have a problem...

the point is that image_attach's content is shown by default AFTER the node content block. i added provvided snippet to node.tpl.php to print it BEFORE content block, and it works

but (obviously) the default one after the block is still showing and now i have two copies of the attached thumbnail in each node....

drewish’s picture

you should update to the dev branch. the teaser is now put into the $node->content array so you can output everything in the order you'd like in your theme function now.

The.Clue’s picture

i _have_ the dev branch on...but attached img (in full node page view) is still at bottom, after the content body itself...

for now i fixed it with CSS (shrinking the content width and absolute positioning thumbnail at top of node container) but its not an elegant solution at all...

dmjossel’s picture

Title: How to output image attached with image_attach? » attached image output below teaser

I've also got the dev branch on, and I cannot reposition the attached images using absolute positioning because teasers aren't all the same size.

Can the above poster be specific about where the attached image is in $content now and how to output it?

theme_image_attach_teaser used to output the teaser image with the node teaser itself appended. Now, it just outputs the teaser image. The node display function in the theme I'm using just outputs $content-- there's no call there to theme_image_attach_teaser. I assume some core module is doing that, and is passing $content with the teaser and teaser image in that order, and I can't see where to override it.

drewish’s picture

marked http://drupal.org/node/129175 as a duplicate

drewish’s picture

Status: Active » Needs review
StatusFileSize
new5.31 KB

i'm not sure i really like this approach but it should do what everyone wants. it adds two settings to the node type page so you can select the weight of the attached image for the teaser and body.

drewish’s picture

Title: attached image output below teaser » Make the attached image's postion in teaser and body configurable
Category: support » feature
dman’s picture

StatusFileSize
new1.51 KB

FWIW, I just rolled a similar (almost identical) solution before coming here to check the issues.

Mine is simpler (in a bad way) and doesn't allow for different settings for teaser vs bodies.
It works for me, so I'll post for reference.

(it's fun playing with the node content array nowadays)

drewish’s picture

my patch allows you to configure the weight on a per-node-type basis, which would be useful when trying to set the order with multiple CCK nodes.

dman’s picture

Yeah, I like yours. It adds a few things I hadn't considered.

drewish’s picture

marked http://drupal.org/node/120267 as a duplicate of this

drewish’s picture

StatusFileSize
new3.32 KB

here's a re-roll.

bloomaniac’s picture

wouldn't it be great to have a filter like http://drupal.org/project/image_filter to put the thumbnail where you want?

drewish’s picture

bloomaniac, feel free to use image_filter, nothing is stopping you. this patch is for a specific purpose.

dman’s picture

If anybody wants a vote, I say +1 and roll with it. It's the way it should be, using weights like this.

drewish’s picture

StatusFileSize
new3.53 KB

that last patch had a bug in it. i kind of thing if this much configuration info is going in, it would make sense to let the admin specify which size image is used in the teaser and body. i might take a swing at that before committing to this.

drewish’s picture

StatusFileSize
new3.39 KB

whoops that had some test code in it. here's the right version.

drewish’s picture

http://drupal.org/node/135801 might be a duplicate of this

drewish’s picture

StatusFileSize
new4.24 KB

here's a re-roll to keep it applying correctly. unless i get more feedback i'm probably just going to go a head and commit this.

dman’s picture

Just do it. It's the Right Thing(TM)

drewish’s picture

Status: Needs review » Fixed

alright, it's committed.

Anonymous’s picture

Status: Fixed » Closed (fixed)