【问题标题】:how to extend kartik fileInput widget for yii2如何为 yii2 扩展 kartik fileInput 小部件
【发布时间】:2017-10-05 19:54:40
【问题描述】:

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

在 common\components 中,我有一个名为 FileInputOveride.php 的文件:

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

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

编辑 - 这里还有一些代码:

这是 _form.php 顶部的声明,它使用了 fileInput

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

谢谢大家!

【问题讨论】:

  • 您是否在模板文件中添加了“使用 common\components\FileInputOveride”而不是包含默认文件?确保使用新名称调用插件。
  • 是的,我尝试将其单独包含在内,但没有任何效果。我还在原版之后用“使用”来称呼它,没有效果。
  • hmm...你能告诉我你使用这个插件的文件吗?
  • @Joint Ive 发布了更多代码,希望这是您需要的。任何帮助表示赞赏。
  • 在你的 _form.php 文件中使用 "FileInputOveride::classname()" 而不是 "FileInput::classname()" - 然后你可以删除用于 kartik 输入的 use 行;)

标签: php yii yii2 yii2-advanced-app


【解决方案1】:

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

【讨论】:

  • 这正是所需要的,不敢相信我没有注意到那部分。谢谢!
  • 我很高兴能帮上忙 :) 你能把我的回答标记为有用吗? :)
  • 我只是将其标记为已回答,显然我没有足够的积分来支持它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
相关资源
最近更新 更多