Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在Drupal 7中更新文件字段_File_Drupal_Field - Fatal编程技术网

File 如何在Drupal 7中更新文件字段

File 如何在Drupal 7中更新文件字段,file,drupal,field,File,Drupal,Field,在hook_user_update中,我正在检索一张facebook个人资料图片,并在文件管理表中保存一条记录。这一切进展顺利。但是我还想为附加到用户实体的文件字段保存一条记录。通常我在这些钩子中所做的是将值赋给$edit['field_something'],我认为这是正确的方法。这一直适用于其他字段类型,但不适用于文件字段。您可以看到在函数的末尾,我转储变量以确认我有适合分配给$edit['field_something']的内容——至少,我认为它是合适的。我没有得到任何错误,但在field

在hook_user_update中,我正在检索一张facebook个人资料图片,并在文件管理表中保存一条记录。这一切进展顺利。但是我还想为附加到用户实体的文件字段保存一条记录。通常我在这些钩子中所做的是将值赋给$edit['field_something'],我认为这是正确的方法。这一直适用于其他字段类型,但不适用于文件字段。您可以看到在函数的末尾,我转储变量以确认我有适合分配给$edit['field_something']的内容——至少,我认为它是合适的。我没有得到任何错误,但在field\u data\u field\u 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;

      }
    }

  }

}

我相信它不起作用的原因是因为在hook_user_update中,更新已经发生了。因此,为$edit设置一个值没有效果。我在问题中发布的相同代码在hook_user_presave中运行良好