3

我正在尝试将节点从我的论坛导入drupal 7。不是批量,而是一个接一个,以便可以创建新闻帖子并将其引用回论坛。更重要的是,我也想带上图片附件......

到目前为止,使用这里的代码示例http://drupal.org/node/889058#comment-3709802大部分工作:创建节点,但图像不经过任何验证或处理。

我希望根据内容类型中定义的规则验证附加的图像。特别是与我的图像字段相关的样式,将它们调整为 600x600。

因此,我决定使用 hook_node_prepare修改“新”节点并使用现有表单创建新内容(基于传入的 url 参数),而不是简单地使用我自己的表单以编程方式创建节点。这非常有效,并且预先填写了我的所有数据的创建表单。包括图片!非常可爱。

我希望我可以点击预览保存,并且所有验证和调整大小都会发生在我的图像上,但我得到了错误:

"The file used in the Image field may not be referenced."

原因是我的文件在 file_usage 表中没有条目.. *le sigh*

那么,当我手动选择要上传的文件时,如何进行所有漂亮的验证和处理?像调整大小一样,file_usage 表中的一个条目。

ajax 上传功能可以做到这一点,但我在 api 的任何地方都找不到调用来执行此操作的代码。

Drupal 调用了哪些文件上传/验证功能,而我没有这样做?

有人对 Drupal 7 的文件/图像 api 有任何经验,可以帮助我吗?

4

1 回答 1

1

为了获取使用条目(本质上,将文件签出到特定模块,以便在使用时不会被删除)查找 Drupal 函数'file_usage_add()'

为了验证传入的图像,我从 user.module 获得了这个示例(如果您对 PHP 感到满意,您可以随时查看核心以了解某些事情是如何通过“Drupal 方式”完成的):

function user_validate_picture(&$form, &$form_state) {
  // If required, validate the uploaded picture.
  $validators = array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
    'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
  );

  // Save the file as a temporary file.
  $file = file_save_upload('picture_upload', $validators);
  if ($file === FALSE) {
    form_set_error('picture_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => variable_get('user_picture_path', 'pictures'))));
  }
  elseif ($file !== NULL) {
    $form_state['values']['picture_upload'] = $file;
  }
}

该函数被添加到 $form['#validate'] 数组中,如下所示:

$form['#validate'][] = 'user_validate_picture'

于 2011-03-01T21:12:36.343 回答