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