Community & Support

CCK Image Field in a block view

I installed the imagefield module for CCK, which is awesome, but what I attempted to do was make a block view with the CCK Image field as a field, and the problem is that the image never changes. It stays on the first image I uploaded and won't change when I go to a different page with a different image.

Comments

EDIT: I meant it shows ALL

EDIT: I meant it shows ALL CCK images, not one. I need it to show only the one specific to the page it's on.

Ah, gotcha. Thanks for the

Ah, gotcha. Thanks for the clarification. See my post below. :)

Is this a block view you

Is this a block view you created with the views module? If so, an export of your view would be helpful. By default, in a generic case, Views will give you the same content regardless of what node you're viewing, unless you do some custom argument handling or theming.

If you just want to have the image for the current node displayed in a block rather than in the node template, you could just create a new (non-views) block with PHP something like the following:

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if (
$node->type == "my_node_type") {
      if (
$node->field_my_image_field) {
        print
$node->field_my_image_field[0]['view'];
      }
    }
  }
}
?>

You can find out what the name of the variable to use is by temporarily inserting

<?php
print_r
($node);
?>

(don't do this on a production site).

Apologies if this isn't what you're asking. More details will help.

xjm - yeah that's exactly

xjm - yeah that's exactly what I want to do, just put certain CCK fields in a block rather than in the node template.

I'm not exactly sure what to do with this PHP code, do I replace the red parts to suit my own needs? Sorry I'm a PHP newb.

Well, you'll want to create

Well, you'll want to create a new block at admin/build/block/add

Then paste the PHP in the block body and set the input format to PHP code.

The parts you should replace are my_content_type and field_my_image_field. These will be the "system" names for the content type and image field, which you can get at admin/content/types and on the "manage fields" tab of your particular content type.

What print_r does is prints out everything in an array or object. If you put this in the block PHP (in the middle of the php snippet, inside all the if statements), it will print every single scrap of information in the node object so you can figure out what variables you can use. Quite a mess, but if you view the page source, it can help you figure out where to start.

I should note that there's probably several ways to do this using css, your page template, or contrib modules out there, but the PHP block is a quick-and-dirty way.

Note: Especially if you're new to PHP, make sure to set the block visibility settings to EXCLUDE the admin pages. Once I made a typo in a block that caused a fatal PHP error and broke my whole site. I had to delete a row manually from the database to get it working again! So now I always put, at a minimum,
"Show on every page except the listed pages:"

admin
admin/*

about how to fix it if you break something

As an aside, for the sake of anyone who ever reads this thread and is wondering about how to fix it if you put something in the block that breaks the whole page, here's what to do. The table that has the custom block data is called boxes.

The MySQL query
select `bid`, `info` from `boxes`;
will show you a list of all the custom blocks you have. Look for the one that's the problem and note its bid.

Say the bid of the problem block was 8. Then you could delete the body of this block and fix the problem with this query:
update `boxes` set `body` = ' ' where `bid` = '8';

Well, I've done all that,

Well, I've done all that, but sadly no block shows up at all. Same thing for the printr function.

Also, I assume you're using "my_node_type" and "my_content_type" interchangeably?

After you create the block,

After you create the block, you'll have to set it to display in whatever region of your page at admin/build/blocks. You can test to make sure the block is getting rendered by putting "test" or something before the PHP tags. You can also put print "test"; inside each if statement to see whether it's getting that far in the code.

To the second question... errr... yes. :) *sheepish*

So, all together with the "tests," PHP for the block would look like this:

This text is just to check whether Drupal is displaying the block.

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  print
"It's a node-like URL <br />";
  if (
$node = node_load(arg(1))) {
    print
"I loaded the node data! <br />";
    if (
$node->type == "my_node_type") {
      print
"It's my kind of node! <br />";
      if (
$node->field_my_image_field) {
        print
"It has an image field defined!";
        print
$node->field_my_image_field[0]['view'];
      }
    }
  }
}
?>

Right, I know how to do all

Right, I know how to do all that. I have a very good understanding of how Drupal works, just not PHP :)

The block won't render with just the PHP code for displaying the CCK fields, but when you add print statements like the code you just posted, the block does render, but only with the print statements in them. The CCK fields don't show up.

P.S. - sorry I'm not responding quickly, but this site is going VERY slow for me right now

Yeah, I've been having a

Yeah, I've been having a spot of trouble with the site as well. Some forms take ten or fifteen minutes to submit! (Cough, cough, scalability improvements.) Anyway.

If everything up through "It has an image field defined!" is printing, then try this:

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if (
$node->type == "my_node_type") {
      if (
$node->field_my_image_field) {
        print
"It has an image field defined! <br />";
        print
"<pre>\n";
       
print_r($node->field_my_image_field);
        print
"</pre>\n";
      }
    }
  }
}
?>

That will print out everything in the array for the image field if the variable is defined. If it does, you can post the output here in a code tag (if you can get the site to load!) so we can figure out which variable you need.

If it's not printing "It has an image field defined!" then try this:

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if (
$node->type == "my_node_type") {
      print
"The node is the right type. <br />"

     
print "<pre>\n";
     
print_r($node);
      print
"</pre>\n";
      }
    }
  }
}
?>

Here's the

Here's the output:

Array
(
    [0] => Array
        (
            [fid] => 6
            [title] => wordpresslogo.jpg
            [alt] => wordpresslogo.jpg
            [nid] => 2
            [filename] => wordpresslogo.jpg
            [filepath] => files/wordpresslogo.jpg
            [filemime] => image/jpeg
            [filesize] => 31179
        )

)

I would have said by now, if the site was not so slow, that I got the filepath argument to print out, and that maybe I could add in some html and make it <img src="[filepath]">. That should print out the correct image.

However, I intend to put other fields in this block as well. For example, a field called "license". Here's the array for it:

Array
(
    [0] => Array
        (
            [tid] => 1
        )

)

Unfortunately, tid is the only argument it seems I can output, and it's not the one I need. Any idea how to get around this problem?

Ok I see what's going on

Ok I see what's going on here now. It's basically the same thing the contemplate module does. But for some reason, the fields are not showing up. Could the contemplate module be conflicting with this somehow?

Hmm, I don't think so. As

Hmm, I don't think so. As far as I know, contemplate just works like a template file to override the node body and teaser, so blocks wouldn't be affected. I could be wrong, though. I suppose you could temporarily disable your template to check.

Slight adjustment for a Drupal 5 context

This is great, but I don't think Drupal 5 supports 'view'. This is what worked for me in D5:

<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if (
$node->type == "my_node_type") {
      if (
$node->field_my_image_field) {
        print
'<img src="' . $node->field_my_image_field[0]['filepath'] . '" alt="generic non-dynamic alt text" />';
      }
    }
  }
}
?>

SUCCESS!

Ok, I think I got it all figured out. The reason "tid" is the only value I can use for the license field is because license a taxonomy term called up from using the CCK Taxonomy module. If I just create a normal field without the CCK Taxonomy module, I am permitted to use the ['value'] value in the block!

Now the only issue for me is, how do I get the block to output <img src="[filepath]">. I know you can integrate HTML into your PHP statements but I'm not exactly sure how. Could you point me in the right direction?

Ok, I was able to get it to

Ok, I was able to get it to output the image using this code:

<img src="<?php
if(arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if (
$node->type == "cms") {
      if (
$node->field_image) {
        print
$node->field_image[0]['filepath'];
      }
    }
  }
}
?>
">

Is there a simpler way I'm overlooking? It seems like there should be.

Oh, and thank you so much for your help man, I really appreciate it.

Well, it amounts to same

Well, it amounts to same thing, but the other way would be to do something like this:

<?php
if (arg(0)=='node' && is_numeric(arg(1)) && (arg(2)!='edit') && (arg(2)!='delete')) {
  if (
$node = node_load(arg(1))) {
    if ((
$node->type == "cms") && $node->field_image) {
      print
"<img src=\"{$node->field_image[0]['filepath']}\">\n";
     
// or print "<img src=\"" . $node->field_image[0]['filepath'] . "\">\n";

   
}
  }
}
?>

The \" escapes the quotation mark so it can be used inside the string, and the {} around the node object tell it to evaluate the whole expression as a variable. (. sticks separate string pieces together in the format in the comment). \n is just a line break to make the HTML source look pretty.

If you want to print your taxonomy term and still store it as a taxonomy term, you could do that one of two ways. The $node object should (I think) include all the term information for the node in $node->taxonomy. So you could do something like

<?php
if ($node->taxonomy && $node->field_license) {
  print
$node->taxonomy[$node->field_license[0]['tid']]->name;
}
?>

If that doesn't work you could use the API function to load the term information:

<?php
if ($node->field_license) {
  if (
$license_term = taxonomy_get_term($node->field_license[0]['tid']) {
    print
$license_term->name;
  }
}
?>

The disadvantage of the second method is that it might cause an extra database query if Drupal doesn't have the taxonomy information cached.

Cheers. :)

Is there any way I can get

Is there any way I can get the term to print in my block without having to assign a field to it? I'd like to just be able to assign a taxonomy terms to the node and have the block fetch those terms somehow. That way I don't have to select the term twice.

yep

$node->taxonomy will contain all the taxonomy term information for the node, regardless of whether you tag it with CCK fields or the normal way. Each term in $node->taxonomy will include the id for the parent term, the id for the vocabulary, the term label, and so forth.

I just found a topic with

I just found a topic with the perfect code that does exactly what I need.

http://drupal.org/node/81035

using imagecahce presets

This has helped me immeasurably! Thanks.

Do you know of a way to display a specific imagecache preset instead of the original that is stored with the imagefield?

image cache

<?php print theme('imagecache', 'Node_Image_thumbnail', $teacher_select->field_teacher_photo[0]['filepath']) ?>

This calls the imagecache presets 1st imagecache - the basic imagecache prefix and 2nd my custom setting, then it calls the image source.
It should be possible to add title field and alt field too, im not yet sure how though...

p.s

read this link: http://drupal.org/node/163561 (especially bottom of the page)

and:
<?php print theme('imagecache', 'Node_Image_thumbnail', $teacher_select->field_teacher_photo[0]['filepath'], $teacher_select->field_teacher_photo[0]['description'],$teacher_select->field_teacher_photo[0]['description']); ?>
echoed my alt and title text -> description is the only name giver in my printout so i've used this for both title and alt tags....

hope it helps!