1

hook_user_update では、Facebook のプロフィール写真を取得し、レコードを file_managed テーブルに保存しています。そのくらい順調です。しかし、ユーザー エンティティに添付されたファイル フィールドのレコードも保存したいと考えています。通常、これらのフックで行うことは、$edit['field_something'] に値を代入することであり、それが正しい方法だと思います。これは他のフィールド タイプでは常に機能しますが、ファイル フィールドでは機能しません。$edit['field_something'] に割り当てるのに適したものがあることを確認するために、変数をダンプする関数の最後の方を見ることができます。少なくとも、それは適切だと思います。エラーは発生しませんが、field_data_field_image テーブルにレコードが作成されません。私は何が欠けていますか?ありがとうございました!

/**
 *  Implementation of hook_user_update().
 *  
 */
function hifb_user_update(&$edit, $account, $category) {

  if (empty($account->field_image['und'])) {

    $fbid = $edit['field_facebook_id']['und'][0]['value'];

    if (!empty($fbid)) {

      $url = 'http://graph.facebook.com/' . $fbid . '/picture?type=large';

      if ($file_contents = file_get_contents($url)) {

        $size = getimagesize($url);
        $ext = image_type_to_extension($size[2]); 
        $filename = 'fb_' . $fbid . '_u_' . $account->uid . $ext;

        $uri = file_default_scheme() . '://' . $filename;

        // Saves the file to the default files directory and creates the record in files_managed table.
        $file = file_save_data($file_contents, $uri, FILE_EXISTS_REPLACE);

        //var_dump($file); die;
        /* Here's an example from the var_dump: object(stdClass)#120 (8) { ["fid"]=> string(3) "576" ["uri"]=> string(30) "public://fb_767816596_u_1.jpeg" ["filename"]=> string(21) "fb_767816596_u_1.jpeg" ["filemime"]=> string(10) "image/jpeg" ["uid"]=> string(1) "1" ["status"]=> int(1) ["timestamp"]=> int(1339686244) ["filesize"]=> int(2919) } */

        // Creates record in field_data_field_image table??
        $edit['field_image']['und'][0] = (array)$file;

      }
    }

  }

}
4

1 に答える 1

0

これが機能しない理由は、hook_user_update で更新が既に行われているためだと思います。したがって、$edit の値を設定しても効果はありません。質問に投稿したのと同じコードが hook_user_presave で正常に動作します。

于 2012-06-14T15:48:21.430 に答える