0
votes

Im new to Yii2 and I have a need to modify the registerAssetBundle() function of kartiks FileInput widget in Yii2. I realize this is in the vendor folder, so I wanted to do an override. FYI, this is using the advanced template. Can anyone tell me why I cannot override or what Im doing wrong? Yii just doesnt pick this file up/ doesnt choke/ no errors/ nothing, just goes about its merry way and renders my page as normal.

In common\components I have a file called FileInputOveride.php:

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

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

Edit -Heres some more code:

Here is the declaration at the top of _form.php which is using the 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

Below this line is some view html, until we get to the fileInput field which looks like this:

<?=
                //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);
            ?>

Trying to override the registerAssetBundle() function in this kartik FileInput.php with my own FileInputOveride.php:

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 <[email protected]>
 * @since 2.0
 * @see http://twitter.github.com/typeahead.js/examples
 */
class FileInput extends InputWidget
{

and here is the entire FileInputOveride.php file:

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

}

As an extra, I could also use the syntax to list the assets belonging to a action/view from within its action.

so:

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

Thanks everyone!

1
Did you add "use common\components\FileInputOveride" in your template file instead of include default file? Make sure you call plugin with new name. - Joint
Yes, I tried to include it by itself with no effect. I also called it with ‘use’ after the original with no effect. - Oman
hmm... can you show me the file where you use this plugin? - Joint
@Joint Ive posted more code, hopefully this is what you needed. Any help is appreciated. - Oman
In your _form.php file use "FileInputOveride::classname()" instead of "FileInput::classname()" - then you can remove use line for kartik input ;) - Joint

1 Answers

1
votes

In your _form.php file use "FileInputOveride::classname()" instead of "FileInput::classname()" - then you can remove use line for kartik input. When you extend any plugin then you must call your plugin class name instead of plugin you extend.