如何为 yii2 扩展 kartik fileInput 小部件
how to extend kartik fileInput widget for yii2
我是 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]);
}
}
}
}
}
另外,我还可以使用语法从其操作中列出属于 action/view 的资产。
所以:
public function actionUpdate(){
//show me all the js registered to this page
谢谢大家!
在您的 _form.php 文件中使用 "FileInputOveride::classname()" 而不是 "FileInput::classname()" - 然后您可以删除 kartik 输入的 use 行。当您扩展任何插件时,您必须调用您的插件 class 名称而不是您扩展的插件。
我是 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]);
}
}
}
}
}
另外,我还可以使用语法从其操作中列出属于 action/view 的资产。
所以:
public function actionUpdate(){
//show me all the js registered to this page
谢谢大家!
在您的 _form.php 文件中使用 "FileInputOveride::classname()" 而不是 "FileInput::classname()" - 然后您可以删除 kartik 输入的 use 行。当您扩展任何插件时,您必须调用您的插件 class 名称而不是您扩展的插件。