Im新到Yii2,我需要修改Yii2中kartiks FileInput小部件的registerAssetBundle()函數。我意識到這是在供應商文件夾中,所以我想做一個覆蓋。僅供參考,這是使用高級模板。任何人都可以告訴我爲什麼我不能重寫或我做錯了什麼? Yii只是沒有選擇這個文件/不嗆/沒有錯誤/沒有,只是用它的快樂方式,並呈現我的頁面正常。如何爲yii2擴展kartik fileInput小部件
在我有一個名爲FileInputOveride.php文件共同\組件:
namespace common\components;
use Yii;
use \kartik\file\FileInput;
class FileInputOveride extends \kartik\file\FileInput
{
//...override function, ...do stuff...
編輯-Heres一些代碼:
這裏是在_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覆蓋registerAssetBundle()函數在這個卡爾蒂克FileInput.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
{
,這裏是整個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」而不是包含默認文件?確保你用新名稱調用插件。 – Joint
是的,我試圖自己包括它,但沒有效果。我也在原作之後用'use'調用它,沒有任何效果。 – Oman
嗯......你能告訴我你使用這個插件的文件嗎? – Joint