gpt4 book ai didi

php - 显示上传的图片 - Drupal 7 Form API

转载 作者:行者123 更新时间:2023-12-04 00:40:45 25 4
gpt4 key购买 nike

这是我的表单域,供用户上传他们公司的标志:

$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),
),

我想做的是在他们点击“上传”后显示他们的 Logo 。

我很惊讶这不是内置到表单 API 中的一个简单选项...如何才能做到这一点?

最佳答案

声明一个主题函数

/**
* Implements mymodule_thumb_upload theme callback.
*/
function theme_mymodule_thumb_upload($variables) {

$element = $variables['element'];

if (isset($element['#file']->uri)) {
$output = '<div id="edit-logo-ajax-wrapper"><div class="form-item form-type-managed-file form-item-logo"><span class="file">';
$output .= '<img height="50px" src="' . image_style_url('thumbnail', $element['#file']->uri) . '" />';
$output .= '</span><input type="submit" id="edit-' . $element['#name'] . '-remove-button" name="' . $element['#name'] . '_remove_button" value="Remove" class="form-submit ajax-processed">';
$output .= '<input type="hidden" name="' . $element['#name'] . '[fid]" value="' . $element['#file']->fid . '"></div></div>';
return $output;
}
}

告诉 drupal 这个主题函数可以用来渲染一个元素。

/**
* Implements hook_theme().
*/
function mymodule_theme() {
return array(
'mymodule_thumb_upload' => array(
'render element' => 'element',
)
);
}

使用 '#theme' => 'mymodule_thumb_upload', 使托管文件调用元素的自定义主题函数。

<?php
$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'],
'#theme' => 'mymodule_thumb_upload',
'#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),
),

关于php - 显示上传的图片 - Drupal 7 Form API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18997423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com