yii2 ckeditor custom plugins - ckeditor

I'm using 2amigos ckeditor plugins in yii2, I was able to create a sample plugins from plugin_sdk_sample, it works fine in a raw project, but when I put that in a yii2 project, the button doesn't appear.
I put the custom plugin in \vendor\2amigos\yii2-ckeditor-widget\src\assets\ckeditor\plugins\ with plugin.js and png icon with the folder structure as described in the guide. I think the problem is with adding it to config.
I tried following in vendor\2amigos\yii2-ckeditor-widget\src\assets\ckeditor\config.js
CKEDITOR.editorConfig = function( config ) {
config.extraPlugins = 'timestamp';
};
also tried the following in view:
<?= $form->field($model, 'content')->widget(CKEditor::className(), [
'clientOptions' => ['config.extraPlugins' => 'timestamp'],
'options' => ['rows' => 6],
'preset' => 'basic'
]) ?>
but none of them seems to work and showing the button, what am I doing wrong here?

I think you have to add plugin.js into the list of script in
class CKEditorAsset extends AssetBundle
{
public $js = [
'ckeditor.js',
'plugin.js',
'adapters/jquery.js'
];

You can also customize the yii2 plugin toolbars like mentioned in below url-
dosamigos\ckeditor\CKEditor custom toolbar

<?= $form->field($model, 'content')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'custom',
'clientOptions' => [
'extraPlugins' => 'timestamp',
]
]) ?>

I tried this in the file
"vendor/2amigos/yii2-ckeditor-widget/src/CKEditorAsset.php"
public $sourcePath = '#bower/adminlte/plugins/ckeditor';

Related

Validate Select2 in Yii2 via AJAX

I have Yii2 application which uses the Kartik plugin to initialize Select2 dropdowns in forms.
I have one particular Select2 which uses AJAX call to get the data for the drop down options.
<?=
$form->field($model, 'court_house_id', ['enableAjaxValidation' => true, 'selectors' => ['input' => '#' . $id . "-court-house"],'template' => FormHelper::GenerateFieldTemplate([6])])
->widget(Select2::classname(), [
'options' => ['id' => $id . "-court-house", 'placeholder' => Yii::t('app', 'Search court house...')],
'hashVarLoadPosition' => \yii\web\View::POS_READY,
'pluginOptions' => [
'dropdownParent' => new JsExpression("$('#$modalWindowId')"),
'allowClear' => true,
'minimumInputLength' => 2,
'language' => [
'errorLoading' => new JsExpression("function () { return '" . Yii::t('app', 'Search...') . "'; }"),
],
'ajax' => [
'url' => app\components\UrlMaker::link('data/court-house-list'),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(courthouse) { return courthouse.text; }'),
'templateSelection' => new JsExpression('function (courthouse) { return courthouse.text;}'),
]])
->label(Yii::t('app', 'Court House'), ['class' => FormHelper::GenerateLabelClassTemplate([3])]);
?>
Intentionally pasting all of the code, although most of it is irrelevant I would assume.
I have this loaded in multiple dynamically created forms thus all the strange ids and selectors. However, the form has different dropdown which controls whether some of the fields are shown (and required) or not. This particular field above is only shown in one of the scenarios which all the other variations of the form do not have it. So the model has the following validation:
[['court_house_id', 'staff'], 'required', 'on' => self::SCENARIO_ONE],
By the way staff is just a regular text field and everything works for it.
In order to change the scenario, I have the following line in the view with the form:
<?php $model->scenario = \app\models\MyModel::SCENARIO_ONE; ?>
The problem is that when I submit the form empty, the staff field gets marked in red as invalid but the court house is marked in green as valid although it is empty.
If I go into the model and remove the 'on' => self::SCENARIO_ONE part then everything works as expected - on empty submit the court house field also lights up in red but that would be a problem for the rest of my scenarios where this field is not needed.
Any ideas what might be causing the problem and how to resolve it?
Try to set the scenario in controller before calling save() method, for example
$model = new MyModel(['scenario' => MyModel::SCENARIO_ONE])

Dropdownlist throwing too few args error when including a where statement in Yii2

I am trying to use a dropdownlist in an active form which includes some where statements. I have tried so many variaions and im stumped.
Here is the code block
echo $form->field($model, 'pid')->dropDownList(
ArrayHelper::map(Products::find()
->where('pid','43')
->andFilterWhere(['like', 'product_name', 'window'],
[
'prompt' => 'Operating System',
])));
I am not sure how to go about this.
Refer Yii2 ArrayHelper map()
echo $form->field($model, 'pid')->dropDownList(
ArrayHelper::map(Products::find()
->where(['pid' => '43'])
->andFilterWhere(['like', 'product_name', 'window'])->all(), 'your_id', 'your_name'),
[
'prompt' => 'Operating System',
]);

CKeditor doesn't work with content rendered with renderAjax()

I installed CKeditor for Yii2 according to extension docs.
I have pages in rendered in controller for eg.:
public function actionTest($id)
{
$model = $this->findModel($id);
return $this->renderAjax('/test', ['model' => $model]);
}
CKeditor loads properly if it is via:
return $this->render('/test', ['model' => $model]);
but does not load if it is loaded via renderAjax(). Seems to be CKeditor's jquery missing in this content. May I know how to add it to this page?
In my view:
<?= $form->field($model, 'Desc')->label('Description'. Html::tag('span', '*',['class'=>'required']))->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'basic'
])
?>
In case of dynamically loaded textarea you need to reinitialize CKEditor in AJAX success callback.
It can be done like so:
CKEDITOR.replace('id-of-your-textarea-field');
Links:
Cannot convert dynamically loaded teaxtarea into ckeditor
Official docs

How to translate form labels in Zend Framework 2?

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.
Thank you in advance!
class Search\Form\CourseSearchForm
...
class CourseSearchForm extends Form {
...
public function __construct(array $cities) {
parent::__construct('courseSearch');
...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
...
}
}
view script /module/Search/view/search/search/search-form.phtml
<?php echo $this->form()->openTag($form); ?>
<dl>
...
<dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
<dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->
The module/Application/config/module.config.php is configured:
return array(
'router' => ...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => ...
'view_manager' => ...
);
I also edited my view and use the FormLabel view helper:
<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>
Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.
But it's still not working.
EDIT
The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.
The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.
Instead of using:
<?php echo $form->get('city')->getLabel(); ?>
You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Once you do use the formlabel view helper:
echo $this->formLabel($form->get('city'));
And of course make sure your translations are in your .po file.
i think your problem is that you label are not detected by poedit (or similar tool), so you have to add them manually to your poedit catalogs (.po)
to make your label strings detected by tools like poedit, your strings need to be used inside a translate() function or _() (other function can be added in Catalog/properties/sources keyword)
as the _() function is not user in ZF2 (today) so a tiny hack is to add a function like this in your index.php (no need to modify anything, this way, in poedit params):
// in index.php
function _($str)
{
return $str;
}
and in your code, just use it when your strings are outside of a translate function
//...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => _('myLabel') , // <------ will be detected by poedit
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
//...
or like this if you prefer
$myLabel = _('any label string'); // <--- added to poedit catalog
//...
'options' => array(
'label' => $myLabel ,
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
#Ruben says right!
Me I use PoEdit to generate my *.mo files and to be sure to get all translations in the file, I create somewhere (in view for example) a file named _lan.phtml with all text to be translated :
<?php echo $this->translate("My label");
... ?>
Of course, Poedit has to be configured to find my keywords. check this to how to configure it
All solutions don't use the power of ZF2. You must configure your poedit correctly :
All things are here :
http://circlical.com/blog/2013/11/5/localizing-your-twig-using-zend-framework-2-applications

How to add wysiwyg editor in pyrocms module

I have developed a PyroCMS custom module, but now I want to add WYSIWYG editor instead of the text area.
How can I add it?
Just append this while building the template in the controller.
->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
and then
echo form_textarea(array('id' => 'body', 'name' => 'code', 'value' => '', 'rows' => 30, 'class' => 'wysiwyg-advanced'));
Hopefully this will help you sometime..
Update:
With PyroCMS 2.2, $this->data has been deprecated.
In your controller you would need to change
->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
to
->append_metadata($this->load->view('fragments/wysiwyg', compact('items'), TRUE))

Resources