Hia good themers,

I've been struggling for hours with something here, and could need a push!

I've recently jumped ship from the core profile module to Content Profile. I've set up a profile with a CCK Imagefield as the profile picture, and are now trying to theme my node.tpl.php and comment.tpl.php to use this Imagefield (which is set up with a default picture) there. Also, i use Imagecache to resize these.

First, in my content_profile-display-view.tpl.php (the theme file for Content Profile) I've set up this:

print theme('imagecache', 'profilepic_profilepage', $node->field_profilepicture[0]['filepath'], $alt, $title, $attributes);

Which works just as expected. If the user has set a picture it shows in the right Imagecache preset, and if not the default image from Imagefield is displayed.

However, if I use this code in node.tpl.php:

 // load the content profile object for the node author
   $account_picture = content_profile_load('profile', $node->uid); 
   print theme('imagecache', 'profilepic_comments', $account_picture->field_profilepicture[0]['filepath'], $alt, $title, $attributes); 

or similar in my comment.tpl.php, the default picture is *not* printed if the user has not set one, otherwise it works just fine.

Also, I've experimented with this:

if ($account_picture->field_profilepicture[0]['filepath']) {
  print theme('imagecache', 'profilepic_comments', $account_picture->field_profilepicture[0]['filepath'], $alt, $title, $attributes);
} else {
  print theme('imagecache', 'profilepic_comments', $default_picture, $alt, $title, $attributes);
}
;

Which also works if i hard code the path to the default picture where I have $default_picture above. However, I'd like to assign $default_picture to whatever the default picture for my specific Imagefield is, and I can find no way to do that :/

With the core profile module you could do this:

$default_picture = variable_get('user_picture_default', '');

..but I find no way to do a similar thing for the default value of an Imagefield..

so:

  • Anyone knows why Imagefield prints the default image if nothing set in one template file, but not the two others, and / or
  • Anyone knows how you can populate a variable for an Imagefield's default picture as described above, or
  • Any other pointers?

:)

Comments

ryan_courtnage’s picture

Hi, did you figure out how to programatically fetch the default value for an imagefield?

vegardjo’s picture

Hia, sadly no, I still use an absolute reference to the default file in the file system..

aac’s picture

Subscribing!!

---~~~***~~~---
aac

Jeff Burnz’s picture

Subscribing, gonna take a look a little later, just bookmarking so I don't loose track of it. Good question btw.

Christopher Camps’s picture

node_load() by itself will not initialize default images for cck imagefields. I don't know about which template files load nodes in what way but here's what solved it for me:

<?php
$node = node_build_content($node);
print theme('imagecache', 'preset', $node->field_myimagefield[0]['filepath']);
?>
entrigan’s picture

If you do not want to build the whole node you can user

 $field = content_fields('field_[name of field]', '[node_type]');
$image = theme('image', $field['widget']['default_image']['filepath']); 

to get the default image

ndvo’s picture

That should work for D6, but there is no content module for D7. Use the field_info_fields() for the same purpose. It'll return an array of all the available fields with their data.

Here is a way to get the uri for the default image of a FOOBAR field in D7.

<?php
    $field = field_info_fields();
    $uri = file_load($field['field_FOOBAR']['settings']['default_image'])->uri;
?>
quasidynamic’s picture

I noticed that default image data is only loaded into the field data when the node IS the content. If the node is related or 'node_load'ed, the field is an empty array, which is unfortunate.

So in my node--BUNDLE.tpl.php I added this little snippet in an if/else to check for image data and to load the default if it's empty. a little bit hackish but works a treat! thanks!

if (empty($content['body']['#object']->field_BUNDLE[LANGUAGE_NONE][0]['entity']->field_IMAGEFIELD)) {
	$field = field_info_fields();
	$uri = file_load($field['field_IMAGEFIELD']['settings']['default_image'])->uri;
}
else {
	$uri = $content['body']['#object']->field_BUNDLE[LANGUAGE_NONE][0]['entity']->field_IMAGEFIELD[LANGUAGE_NONE][0]['uri'];
}

print theme('image_style', array( 'path' => $uri .... ));
vegantriathlete’s picture

According to https://api.drupal.org/api/drupal/modules!field!field.info.inc/function/... Use of this function should be avoided when possible, since it loads and statically caches a potentially large array of information. Use field_info_field_map() instead.

Also, according to the above api document you may be better off using field_info_field.

jan.ptacek’s picture

it operates on the node passed by reference, so better do:
node_build_content($node);

anjjriit’s picture

I have 2 preset called
1. not-sold-image
2. sold-image
and taxonomy term
1. not sold
2. sold
How to set imagecache in my node.tpl.php if i wanto display image preset depend on taxonomy term?
so if taxonomy terms not sold were fulfilled imagecache not-sold-image will displayed, in otherword if if taxonomy terms sold were fulfilled imagecache sold-image will displayed ?

thank you for your assistance greatly appreciated
anjjriit - cipta-mobil.

serjas’s picture

1. Open template.php in your theme folder . If your theme dont have template.php, create it
2. Add

/**
* load default image from image field
*/
function themename_default_field_image_path($field_name) {
	$result1 = db_query("SELECT id FROM {field_config} WHERE field_name = :field_name",
	array(
		':field_name' => $field_name,
		));
	foreach ($result1 as $row) {
		$id = $row->id;
	}
	$type = 'default_image';
	$result = db_query("SELECT fid FROM {file_usage} WHERE type = :type AND id = :id",
	array(
		':type' => $type, ':id' => $id,
	));
	foreach ($result as $row) {
		$img = $row->fid;
		$img = file_load($img);
		$img = $img->uri;
		$img = file_create_url($img);
	}
	return $img;
}

3 . Save template.php

Now you can call themename_default_field_image_path($field_name) in any tpl files
for eg:

echo themename_default_field_image_path('field_name');

Correct me if i am doin anything wrong :)

PawelR’s picture

Hi Serjas,

I think what your code does is correct but it's better to use Drupal API which loads field configuration data from the database once and caches it statically instead of performing additional database queries to fetch again something what has been already fetched and is easily accessible via field_info_field().

This function can be reduced to something like this:

function themename_default_field_image_path($field_name) {
    $field_info = field_info_field($field_name);
    $fid = $field_info['settings']['default_image'];
    $image = file_load($fid);
    return file_create_url($image->uri);
}

(I believe it works but haven't tested it)

BTW if you want to fetch a single record from a database query it's simpler to use fetchField method instead of a loop:
https://api.drupal.org/api/drupal/includes!database!database.inc/functio...