/** * @return string * @throws NotAcceptableHttpException */ public function run() { if (!Yii::$app->request->isAjax) { throw new NotAcceptableHttpException('This action AJAX only!'); } $post = Yii::$app->request->post(); $keys = array_keys($post); $formName = $keys[0]; if (!isset($post[$formName][$this->textFormatField])) { throw new InvalidParamException('Invalid POST data.'); } $format = $post[$formName][$this->textFormatField]; $text = $post[$formName][$this->textField]; if ($this->context === null) { $this->context = Yii::$app->controller; if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { $this->context = Yii::$app->controller->module; } } if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { throw new InvalidParamException('Invalid context. Add TextFormatsBehavior to module.'); } $formats = $this->context->textFormats; if (!isset($formats[$format])) { throw new InvalidParamException('Format not found.'); } $params = ['fieldName' => $formName . '[' . $this->textField . ']', 'text' => $text, 'formatInfo' => $formats[$format], 'widgetOptions' => $this->context->textEditorWidgetOptions]; if ($this->view) { return $this->controller->renderAjax($this->view, $params); } else { return $this->controller->renderAjax('@vendor/maddoger/yii2-textformats/views/changeFormat.php', $params); } }
/** * @throws \yii\base\InvalidConfigException */ public function init() { parent::init(); if (!$this->changeFormatMessage) { $this->changeFormatMessage = Yii::t('app', 'Are you sure want to change text format and load another editor?'); } if (!$this->changeFormatUrl) { $this->changeFormatUrl = ['change-format']; } if ($this->context === null) { $this->context = Yii::$app->controller; if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { $this->context = Yii::$app->controller->module; } } if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { throw new InvalidParamException('Invalid context. Add TextFormatsBehavior to module.'); } }
/** * @throws \yii\base\InvalidConfigException */ public function init() { parent::init(); if (!$this->format && $this->hasModel() && $this->formatAttribute) { $this->format = $this->model->{$this->formatAttribute}; } if ($this->context === null) { $this->context = Yii::$app->controller; if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { $this->context = Yii::$app->controller->module; } } if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) { throw new InvalidParamException('Invalid context. Add TextFormatsBehavior to module.'); } if ($this->format && !is_array($this->format) && $this->context) { if (!isset($this->context->textFormats[$this->format])) { throw new InvalidParamException('Format not found.'); } $this->format = $this->context->textFormats[$this->format]; } }