【问题标题】:Overwrite Form Control in Drupal 7 Forms API在 Drupal 7 Forms API 中覆盖表单控件
【发布时间】:2015-02-02 08:41:12
【问题描述】:

我的页面中有三个表单控件:字段集、项目类型和托管文件。

function myid_user_page_form(){  
    $form = array();
    $form['id'] = array(
        '#type' => 'fieldset',
        '#title' => t('ID Information'),
        '#collapsible' => TRUE, 
        '#collapsed' => FALSE,
    );
    $form['id']['mymarkup'] = array(
        '#type' => 'item',
        '#markup' => 
            '<canvas id="cnv" name="cnv" width="500" height="100" style="border: 1px solid black;"></canvas>'
    );  
    $form['id']['custom_content_block_image'] = array(
        '#type' => 'managed_file',
        '#name' => 'custom_content_block_image',    
        '#size' => 40,      
        '#upload_location' => 'public://',
        '#theme' => 'myid_signature_upload',
    ); 
}

当我点击“选择文件”按钮并上传图片时,它会是这样的:

我想在第一个表单控件位置/画布位置显示图像。我该怎么做?

这些是我的代码:

/**
  * Implements myid_signature_upload theme callback.
  */

function theme_myid_signature_upload($variables) {    
    $element = $variables['element'];
    $output = '';  
    if($element['fid']['#value'] != 0) {    
        $output .= '<div class="multifield-thumbnail">';
        $output .= theme('image_style', array('style_name' => 'thumbnail',                   'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
        $output .= drupal_render_children($element);
        $output .= '</div>';
    }
    return $output;  
}
function myid_theme(){
    return array(    
        'myid_signature_upload' => array(
            'render element' => 'element',     
        ),
    ); 
}

【问题讨论】:

  • 您想只显示画布内的图像,还是同时显示上传文件字段?
  • @Muhammad Reda -->我只想在画布位置显示上传的图像并隐藏画布。

标签: file-upload drupal drupal-7 form-api


【解决方案1】:

尝试使用画布标记将上传的图像显示包裹在theme_myid_signature_upload() 中。

function theme_myid_signature_upload($variables) {    
    $element = $variables['element'];
    $output = '';  
    if($element['fid']['#value'] != 0) {
        // open canvas tag <canvas ... >    
        $output .= '<canvas id="cnv" name="cnv" width="500" height="100" style="border: 1px solid black;">';
        $output .= theme('image_style', array('style_name' => 'thumbnail',                   
                   'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
        $output .= drupal_render_children($element);
        // close canvas tag </canvas>
        $output .= '</canvas>';
    }
    return $output;  
}

【讨论】:

  • --> 我试过了,但是画布变成了两个。另一种选择是隐藏画布标签。我该怎么做?
  • myid_user_page_form 表单中删除了画布。它不再需要了。
  • 画布覆盖了我的图像和删除按钮。如何让它再次可见?
  • 我认为这是一个 CSS 问题。您可以使用 css 解决此问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
相关资源
最近更新 更多