2015-02-11 66 views
0

我无法在Drupal 7 Form托管文件上工作图像预览。 我的template.php有这样的代码:Drupal 7 |表单托管文件上传图像预览

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); 

形式本身正在检查附加条件(如userpoints)之后,我能将图像添加到节点(以编程方式),但无法在上传过程中获取图像的预览。我尝试使用#主题,但它似乎根本不起作用。 我的主题功能如下所示:

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>'; 
    } 

任何想法?

回答

0

你需要在模块中用hook_theme声明你的主题。

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