【问题标题】:Drupal 7 - Fill custom form image field with image from databaseDrupal 7 - 用数据库中的图像填充自定义表单图像字段
【发布时间】:2013-09-15 03:57:17
【问题描述】:

我有一个自定义表单模块,其字段类型为“managed_file”,数据库中有一个条目指向图像的文件路径。

我想在表单首次显示时将此图像预加载到“managed_file”字段中。

我尝试将#default_value 分配给图像的文件路径,但这没有任何作用。

最好的方法是什么?

$result = db_query('SELECT n.companyName, n.billingEmail, n.leadEmail, n.contactEmail, n.contactEmail,
                        n.url, n.description, n.companyLogo, n.promoVideoUrl
        FROM {leads_client} n WHERE n.clientId = :uid', array(':uid' => $GLOBALS['user']->uid));    

    $row = $result->fetchAssoc();


$form['company_logo'] = array(
  '#type' => 'managed_file',
  '#title' => t('Company Logo'),
  '#description' => t('Allowed extensions: gif png jpg jpeg'),
  '#upload_location' => 'public://uploads/',
  '#default_value' => $row['companyLogo'],
  '#upload_validators' => array(
    'file_validate_extensions' => array('gif png jpg jpeg'),
    // Pass the maximum file size in bytes
    'file_validate_size' => array(1024*1024*1024),
  ),
);

【问题讨论】:

    标签: drupal drupal-7 nodes form-api


    【解决方案1】:

    默认值需要是文件 ID('managed_file' 的 'managed' 部分表示 Drupal 会为您处理 file_managed 表中的文件,并且对它的任何引用都需要通过 ID) .

    如果您当前将 URL 保存在 companyLogo 中,您可能需要考虑将该列更改为 int 并保存文件 ID。这样你现有的代码就可以工作了。

    如果您需要在任何时候获取 URI,您可以加载文件:

    $uri = file_load($row['companyLogo'])->uri;
    

    如果您需要完整的网址:

    $url = file_create_url($uri);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      相关资源
      最近更新 更多