Drupal 7 |表单托管文件上传图像预览

时间:2015-02-11 10:57:13

标签: forms file api drupal upload

我无法在Drupal 7 Form托管文件上进行图像预览。 我在template.php中有这样的代码:

function testform($form, &$form_state) {
  $form = array();
  $form['con_image'] = array(
        '#type' => 'managed_file',
        '#title' => t('Image'),
        '#required' => TRUE,
        '#default_value' => variable_get('con_image', ''),
        '#progress_indicator' => 'bar',
        '#progress_message' => 'Uploading ...',
        '#upload_location' => 'public://gallery/',
        '#theme' => 'test',
        '#upload_validators' => array(
            'file_validate_is_image' => array(),
            'file_validate_extensions' => array('jpg jpeg'),
            'file_validate_image_resolution' => array('6000x4000','800x600'),
            'file_validate_size' => array(6 * 1024 * 1024),

        ),


  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add to site'),
  );
  return $form;
}

我通过代码

检查其他条件(如用户点)后调用testform
$arr = drupal_get_form('testform');
      print drupal_render($arr);

表单本身正常工作,我可以将图像添加到节点(以编程方式),但无法在上传过程中获取图像预览。我尝试使用#theme,但它似乎根本不起作用。 我的主题功能如下:

function theme_test($variables) {
  $element = $variables['element'];

  $output = '';

  $base = drupal_render_children($element); // renders element as usual

  if($element['fid']['#value'] != 0) {
    // if image is uploaded show its thumbnail to the output HTML
    $output .= '<div class="multifield-thumbnail">';
    $output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
    $output .= '</div>';
  }

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要在模块中使用hook_theme声明主题。

function yourmodule_theme() {
   return array(
     'test' => array(
        'render element' => 'element',
    )
  );
}