如何为yii2扩展kartik fileInput小部件

时间:2017-10-05 19:54:41

标签: php yii yii2 yii2-advanced-app

我是Yii2的新手,我需要修改Yii2中kartiks FileInput小部件的registerAssetBundle()函数。我意识到这是在供应商文件夹中,所以我想要覆盖。仅供参考,这是使用高级模板。任何人都可以告诉我为什么我不能覆盖或我做错了什么? Yii只是没有选择这个文件/没有阻塞/没有错误/什么都没有,只是采用它的快乐方式并正常呈现我的页面。

在普通\组件中,我有一个名为FileInputOveride.php的文件:

   namespace common\components;
   use Yii;
   use \kartik\file\FileInput;

   class FileInputOveride extends \kartik\file\FileInput
   {
     //...override function, ...do stuff...

编辑 - 还有更多代码:

这是使用fileInput

的_form.php顶部的声明
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\bootstrap\Modal;
use kartik\widgets\FileInput; <-- if I take this out, it errors that it cant find ::FileInput
use common\components\FileInputOveride; <--this has no effect

在这行下面是一些视图html,直到我们到达fileInput字段,如下所示:

<?=
                //fileinput widget for single file upload
                 $form->field($model, 'cover_file')->widget(FileInput::classname(), 
                    [
                    'options'=>
                        [
                            'accept'=>'image/*',
                            'multiple' => false, 
                            'id'=>'cover_file',

                        ],
                    'pluginOptions' => 
                        [
                            'uploadUrl' => $upload_url,
                            'maxFileCount' => 1,
                            'allowedFileExtensions' => ['jpg', 'png','jpeg'],
                            'initialPreviewShowUpload' => false,
                            'uploadAsync'=> false,
                            'autoReplace'=>true,

                        ],
                    'pluginEvents' => 
                        [
                            'fileuploaded'=>"function(event, data, previewId, index){
                                 $.get( './call-image?id=".$model->id."', function( response ) {
                                      $('#thumb-container-image').html(response);
                                });
                            }",

                        ],
                ])->label(false);
            ?>

尝试使用我自己的FileInputOveride.php覆盖此kartik FileInput.php中的registerAssetBundle()函数:

namespace kartik\file;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use kartik\base\InputWidget;
use kartik\base\TranslationTrait;

/**
 * Wrapper for the Bootstrap FileInput JQuery Plugin by Krajee. The FileInput widget is styled for Bootstrap 3.x with
 * ability to multiple file selection and preview, format button styles and inputs. Runs on all modern browsers
 * supporting HTML5 File Inputs and File Processing API. For browser versions IE9 and below, this widget will
 * gracefully degrade to normal HTML file input.
 *
 * @see http://plugins.krajee.com/bootstrap-fileinput
 * @see https://github.com/kartik-v/bootstrap-fileinput
 *
 * @author Kartik Visweswaran <kartikv2@gmail.com>
 * @since 2.0
 * @see http://twitter.github.com/typeahead.js/examples
 */
class FileInput extends InputWidget
{

这是整个FileInputOveride.php文件:

namespace common\components;
use Yii;

class FileInputOveride extends \kartik\file\FileInput
{
     /**
     * Registers the asset bundle and locale
     */
    public function registerAssetBundle()
    {
        $view = $this->getView();
        if ($this->resizeImages) {
            PiExifAsset::register($view);
            $this->pluginOptions['resizeImage'] = true;
        }
        $theme = ArrayHelper::getValue($this->pluginOptions, 'theme');
        if (!empty($theme) && in_array($theme, self::$_themes)) {
            FileInputThemeAsset::register($view)->addTheme($theme);
        }
        if ($this->sortThumbs) {
            SortableAsset::register($view);
        }
        if ($this->purifyHtml) {
            DomPurifyAsset::register($view);
            $this->pluginOptions['purifyHtml'] = true;
        }

//above is the existing code          
//below is the additional code i added to this function
      $assetsRegistered =  FileInputAsset::register($view)->addLanguage($this->language, '', 'js/locales');

      //array of pages/paths we dont want to include the asset on
      $pageArray = ['releases/update'];

      //array of assets we dont want to use for the above pages
      $fileArray = ['js/fileinput.js'];

      //for each page, see if the file(s) specified is/are included, if so, unset them in the assets array
      foreach($pageArray as $path)

          if(in_array($path, $pageArray)){

            foreach($fileArray as $file){

                if(in_array($file,$assetsRegistered->js)){
                  $key=  array_search($file, $assetsRegistered->js);
                  unset($assetsRegistered->js[$key]);
                }
            }
        }
    }

}

作为额外的,我还可以使用语法从其操作中列出属于操作/视图的资源。

这样:

public function actionUpdate(){
 //show me all the js registered to this page

谢谢大家!

1 个答案:

答案 0 :(得分:0)

在_form.php文件中使用&#34; FileInputOveride :: classname()&#34;而不是&#34; FileInput :: classname()&#34; - 然后你可以删除kartik输入的使用行。当你扩展任何插件时,你必须调用你的插件类名而不是你扩展的插件。