web-dev-qa-db-ja.com

field_view_field()を使用してユーザープロフィール写真を印刷するにはどうすればよいですか?

ノードページに表示されるブロック内のユーザープロファイルの画像を印刷しようとしています。次のコードのように、ノードの作成者をロードしました。

<?php 
$node = menu_get_object('node');
$user = user_load($node->uid);
?>

これは私がブロックのために思いついたコードです:

<div class="author-block">
  <h4>Meet the author</h4>
  <?php print drupal_render(field_view_field('user', $user, 'picture', 'user-pic-style'));?>
</div>

これは機能せず、何も印刷しません。ユーザーの画像がある場合はユーザーの画像をロードし、ない場合はデフォルトのユーザー画像をロードします。 field_view_field() を使用して問題なく印刷できる他のカスタムユーザーフィールドがあるので、なぜ私は運が悪いのかと思っています。

2
oobie11

これを試して

<div class="author-block">
  <h4>Meet the author</h4>
  <?php print drupal_render(field_view_field('user', $user, 'picture', array('settings' => array('image_style' => 'user-pic-style'))));?>
</div>
2
Jared

フィールドがデータを返さない理由は完全にはわかりませんが、画像であることと、スタイルを設定していないことに関係があると思います。これが私が以前に画像で同様のことをした方法です。元の顧客を保護するためと、これがブロックではなくCtoolsプラグインとして行われたという事実を隠すために、以下のコードを変更したことに注意してください。したがって、スペルミスなどのマイナーな問題が発生する可能性があります。その場合は私の謝罪。

// Create a shortcut to the image we want to render
$image_data = $image_node->field_images[LANGUAGE_NONE][0];

// Image rendering data
$variables = array(
  'style_name' => 'blurb_208x208',
  'path' => $image_data['filename'],
  'alt' => '',
  'title' => '',
);

$styles = image_styles(); // Get all available styles
$style = $styles['blurb_208x208'];

// We are responsible ourselves for ensuring the derived image is available.
$derivative_uri = image_style_path($style['name'], $path_to_original_image);
if (!file_exists($derivative_uri)) {
  image_style_create_derivative(
    $style,
    $image_data['uri'],
    $derivative_uri
  );
}

$markup = '<div class="image-wrapper">' . theme('image_style', $variables) . '</div>';
0
Letharion