Drupal: Vérification pour voir si l'utilisateur A Image
Question
Je l'affichage de la userpicture sur un nœud avec ce code ici:
<?php
$user_load = user_load($node->uid);
$imgtag = theme('imagecache', 'avatar_node', $user_load->picture, $user_load->name, $user_load->name);
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
print l($imgtag, 'u/'.$user_load->name, $attributes);
?>
Cela fonctionne très bien, sauf si l'utilisateur n'a pas de photo, dans ce cas, il semble étrange.
Comment puis-je charger l'image par défaut si l'utilisateur ne dispose pas d'une image. Je ne crois pas avoir accès à l'image $ dans ce bloc.
Merci d'avance.
La solution
$user_load = user_load($node->uid);
$picture = $user_load->picture ? $user_load->picture : variable_get('user_picture_default', ''); //
$imgtag = theme('imagecache', 'avatar_node', $picture, $user_load->name, $user_load->name);
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
print l($imgtag, 'u/'.$user_load->name, $attributes);
si vous copiez l'image par défaut dans le répertoire des fichiers, vous pouvez le déterminer par http: // api.drupal.org/api/function/file_directory_path/6
Autres conseils
est ce que j'utilise une situation similaire:
if (!empty($user->picture) && file_exists($user->picture)) {
$picture = file_create_url($user->picture);
}
else if (variable_get('user_picture_default', '')) {
$picture = variable_get('user_picture_default', '');
}
if (isset($picture)) {
$alt = t("@user's picture", array('@user' => $user->name ? $user->name : variable_get('anonymous', t('Anonymous'))));
$picture = theme('image', $picture, $alt, $alt, '', FALSE);
if (!empty($user->uid) && user_access('access user profiles')) {
$attributes = array(
'attributes' => array('title' => t('View user profile.')),
'html' => TRUE,
);
echo l($picture, "user/$user->uid", $attributes);
}
}
Il est adapté de http: // api.drupal.org/api/drupal/modules%21user%21user.module/function/template_preprocess_user_picture/6
Dans Drupal 7 J'utilise:
global $user;
file_create_url( file_load($user->picture)->uri)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow