I have three node types that I am trying to use image_attach with. Each node type has it's own node template file as I want to customize the layout of the node output. I have noticed that on one type I have to set the image size to "hidden" or I will get two images (my themed, manual attach and the automated attach), on another I have it as "original" and get the desired themed output, and on the last I get only the automated output only and not my themed image.

In each template file I use the following code to output the attached image.
<?php print theme('image_attach_body', $node); ?>
I have also put a "$node = .." in there as well but it makes no difference.

Results by node type
"Story" Node - node-story.tpl.php - Have to set image size (in edit content type) to "Hidden" or I get two images. I do get my themed image.
"Custom" Node Type (CCK node) - node-custom.tpl.php - Have to set image size to "Original" or I get no image. I do get my themed image.
"Example" Node Type - node-example.tpl.php - Have to set image size to "Original" but it is the automated output, NOT my themed image.

Teaser vs. Full article seems makes no difference. (I need to do more testing to be sure.) As you can see, the behavior does not seem to be consistent across node types even though I am implementing them the same. Any ideas on what I am doing wrong?

Thanks!

Comments

joachim’s picture

I'm baffled, as the only thing that depends on the node type is this line:
$img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL);

Can you output that to see what you're getting?

CMatters’s picture

Thanks for the quick reply. Apologies for my delayed response, I've been out of town on business. I'll give your suggestion a try tomorrow and let you know what I get. Thanks again.

CMatters’s picture

Ok.. I put the following code in all 3 of my template files:

<? $img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL); echo $img_size;?>

I did notice some unexpected things.. I need to play around with it a lot more but here's the quick conclusion I came to. On my last node type (example), despite the fact that I am theming a teaser view, image_attach seems to be using the settings for full node.

Initial testing results for this node type as a TEASER:
Teaser/Full both = "hidden", no image displayed, $img_size = "hidden"
Teaser = "thumbnail", full = "hidden", one image is displayed in thumbnail in the non-themed location, $image_size = "hidden"
Teaser = "thumbnail", full = "thumbnail". two images are displayed as thumbnails one in the themed location and the other in the non-themed location, $img_size= "thumbnail"
Teaser = "hidden", full ="thumbnail", one image displayed in the themed location as a thumbnail, $img_size = "thumbnail"

I did get it to display what I wanted after this test... just knowing that even in teaser view, it is using the setting of full node. Perhaps there is something else going on.. I don't know.

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

Obviously you're getting weird things... the variable image_attach_size_body_TYPE is for full body.
For each node type, there are two variables:
- image_attach_size_body_TYPE
- image_attach_size_teaser_TYPE

When you say 'teaser view' -- are you using Views module, or do you mean just a Drupal core list of node teasers such as the front page?

We need to figure out which of theme_image_attach_teaser and theme_image_attach_body is getting called when the problem occurs -- each one respectively checks its appropriate variable. Can you find that out please? Stick as dsm('theme_image_attach_body') in one and likewise for the other. If it's the wrong one, can you do a backtrace?

CMatters’s picture

I thought I was on to something (an error I made) after I read your last message. I was in fact using the wrong image_attach_ for my node. I changed my code to each of the following variants:

<?php print theme('image_attach_teaser', $node); ?>
changed teaser image size to "thumbnail" in the content type. RESULT: two displayed thumbnail images. 1 each in my themed location and one the default.
image size was then changed to "hidden". RESULT: nothing displayed.

<?php print theme('image_attach_thumbnail_teaser', $node); ?>
teaser content size setting set to "thumbnail" RESULT: 1 image displayed in the auto themed location (not my desired location.)
teaser content size setting set to "hidden" RESULT: nothing displayed.

Bottom line, _body and _teaser respectively seem to be pulling the correct image size and displaying appropriately. However, I am still not able to display my themed image in the desired location.

To answer your question, I am using the core drupal teasers for the frontpage. Not using Views for this one. I have some questions for you based on your last message.

1. not sure what the _TYPE is. Can you just give me a brief idea what options there are for that or where I can find more information?
2. can you provide a usage example of dsm('theme_image_attach_body')? I get a drupal error when I put it in.

Thanks again. Are there any docs on the image_attach_ functions anywhere? I've looked but I must be blind.

joachim’s picture

Title: Inconsistent image_attach behavior » restructure theme_image_attach_body/teaser
Category: support » task
Status: Postponed (maintainer needs more info) » Active

_TYPE is the node type. eg 'story'.
dsm is a function in devel module. It's useful.

So you're saying that:
- case 1: if you add your own call to print theme_image_attach_body in the node template, you get the image you're printing yourself AND the one image prints,
- case 2: if you set the image attach display options for that content type to hidden, you get neither.

If you look at the code of theme_image_attach_body/teaser, you'll see it checks image size, and if image size is hidden, it shows nothing.
So all is working as the code is written.

However, this is not ideal.
Surely, if you hide the image, it's because you want to handle theming yourself -- as you're trying to do.

The split between logic and theming isn't good either -- for starters, this has no place in a theming function:

    $image = node_load($node->iid);

We need a function that comes before this, that handles loading the image node etc.
The question would remain of where to find the size required and where best for custom use to call in.

joachim’s picture

Marking #221608: better theming split: can't get to IMG tag or image URL in templates as a duplicate.

A note from there:

The plain URL of the image should be loaded:

        $node->content['image_attach'] = array(
          '#value' => theme("image_attach_{$teaser_or_body}", $node),
          '#weight' => variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0),
joachim’s picture

Status: Active » Postponed

This has to wait for #81102: Attach Multiple Images with image_attach using Drupal upload mechanism.

However, in the meantime, here's what I propose for the theme functions:

- nodeapi: view should take care of node_load() on all of the iids, and also the node_access checks. (strictly speaking this should be done on load op, but then we're carrying around potentially heaps of node objects to the next call -- is that a bad thing?). We also check IMAGE_ATTACH_HIDDEN the current content type options.
- this calls a wrapper theme function with an array of the image data: theme_image_attach($nid, $image_nodes)
- this in turn calls a theme function for each individual image.

joachim’s picture

Status: Postponed » Active

Opened again.

joachim’s picture

Status: Active » Needs review
StatusFileSize
new8.72 KB

Here's a patch.

I've gone for just one theming function -- the teaser/body versions were almost identical apart from where the link goes. If users want to specialize, they can switch on $teaser.
As for theming each image individually, I decided that was overkill. image_display gets called, which calls theme_image_display() so that's available too.

joachim’s picture

joachim’s picture

Waking up the all-new contrib test bot! :)

Status: Needs review » Needs work

The last submitted patch, 412288-image_attach-theme-restructuring.patch, failed testing.

joachim’s picture

Needs a reroll.

joachim’s picture

Version: 6.x-1.0-alpha4 » 6.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new7.18 KB

No reviews after several months.

Here is a reroll which I intend to commit fairly soon.

joachim’s picture

Status: Needs review » Fixed

No reviews in 6 months :( but passes tests -- committed.

#412288 by joachim: Changed structure of theming functions for attached images.

Status: Fixed » Closed (fixed)

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

joachim’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new9.11 KB

Revisiting this in light of #825130: Image attach field in views shows only last attached image per node.

I don't want to hijack the node theme function for Views output, so I'm refactoring this so we have a general theme function that both the node and block theme functions call.

The Views handler can then call this too -- patch for that will be at #825130: Image attach field in views shows only last attached image per node.

Status: Needs review » Needs work

The last submitted patch, 412288-3.image_attach.theme-restructuring.patch, failed testing.

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new9.22 KB

Argh PHP. Why can't you be more like Perl?

joachim’s picture

Status: Needs review » Fixed

Sad panda has no patch reviews but commits this anyway with an increasing cavalier attitude ;)

#412288 by joachim: (Continued) Changed structure of theming functions for attached images, this time with Views support.

Status: Fixed » Closed (fixed)

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

miopa’s picture

+    if (!isset($div_attributes['style'])) {
+      $div_attributes['style'] = '';
+    }
+    $div_attributes['style'] .= 'width: ' . $info['width'] . 'px;';

This hard-coded style makes the default theme not usable if you want to show the pics with different dimension. For example, the thumbnail width is 200px, but in some places in the layout it should be 180px or 160px.

joachim’s picture

> For example, the thumbnail width is 200px, but in some places in the layout it should be 180px or 160px.

Then you need more derivatives...

In general, please open new bug reports for a follow-on such as this.