Home » Php » php – How to disable or remove Yii2 redactor Image and file button?

php – How to disable or remove Yii2 redactor Image and file button?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am using Yii2 Redactor from Here. I want to remove Image and File Upload.

view Code :

<?= $form->field($model, 'reason')->widget(
\yii\redactor\widgets\Redactor::className(), [])

 ?>

Screenshot

Screen shot

How to&Answers:

If you want to hide the buttons for all instances of Redactor you can add this into the module config

'modules' => [
    'redactor' => [
        'class' => 'yii\redactor\RedactorModule',
        'widgetClientOptions' => [
            'buttonsHide' => ['image','file'],
        ]
    ],
],

Otherwise you can add this to the individual call

<?= $form->field($model, 'reason')->widget(\yii\redactor\widgets\Redactor::className(), [
    'clientOptions' => [
        'buttonsHide' => ['image','file'],
    ]
])?>

Answer:

In this library, you just have to update file i.e. Redactor.php at path
yii2-redactor/widgets/Redactor.php

Now update method defaultOptions.
Comment or Remove line from 92 to 103
Here is code of these lines:

$this->setOptionsKey('imageUpload', $this->module->imageUploadRoute);
        $this->setOptionsKey('fileUpload', $this->module->fileUploadRoute);
        $this->clientOptions['imageUploadErrorCallback'] = ArrayHelper::getValue($this->clientOptions, 'imageUploadErrorCallback', new JsExpression("function(json){alert(json.error);}"));
        $this->clientOptions['fileUploadErrorCallback'] = ArrayHelper::getValue($this->clientOptions, 'fileUploadErrorCallback', new JsExpression("function(json){alert(json.error);}"));
        if (isset($this->clientOptions['plugins']) && array_search('imagemanager', $this->clientOptions['plugins']) !== false) {
            $this->setOptionsKey('imageManagerJson', $this->module->imageManagerJsonRoute);
        }
        if (isset($this->clientOptions['plugins']) && array_search('filemanager', $this->clientOptions['plugins']) !== false) {
            $this->setOptionsKey('fileManagerJson', $this->module->fileManagerJsonRoute);
        }