Yii2 Menu widget disable parent item link - yii2-advanced-app

How can I disable only the parent item link and leave its children clickable ? For example - when I click on my parent it should open the submenu and not redirect to somewhere else. And if I click on children it should redirect me.
P.S I red the documentation, searched around google but nothing comes out.
This is my code:
<?php
use backend\models\PageAdmin;
use backend\helpers\BackendPrefix;
use yii\widgets\Menu;
$menu_pages = PageAdmin::find()->where('active=1 AND id_in IS NULL')->all();
$widget_menu_content = [];
foreach ($menu_pages as $key => $page){
$related_pages = PageAdmin::find()->where(['active' => 1, 'id_in' => $page->id])->all();
$widget_menu_content[$key]['label'] = "<i class='{$page->icon}'></i>";
$widget_menu_content[$key]['icon'] = $page->icon;
$widget_menu_content[$key]['url'] = BackendPrefix::PREFIX .'/'. $page->url;
if(!empty($related_pages)){
foreach ($related_pages as $key2 => $rel_page) {
$widget_menu_content[$key]['items'][$key2]['label'] = $rel_page->title;
$widget_menu_content[$key]['items'][$key2]['icon'] = "caret-right";
$widget_menu_content[$key]['items'][$key2]['url'] = BackendPrefix::PREFIX . "/" . $rel_page->url;
}
}
}
?>
<div class="side-mini-panel">
<?=
Menu::widget([
'encodeLabels' => false,
'options' => [
'class' => 'mini-nav',
],
'activeCssClass' => 'selected',
'items' => $widget_menu_content,
'submenuTemplate' => "
\n<div class='sidebarmenu'>
\n<h3 class='menu-title'>{label}</h3>
\n<div class='searchable-menu'>
\n<form role='search' class='menu-search'>
\n<input type='text' placeholder='Search...' class='form-control' />
\n<a href='javascript:void(0)'><i class='fa fa-search'></i></a>
\n</form>
\n<div>
\n<ul class='sidebar-menu'>
\n{items}
\n</ul>
\n</div>",
'linkTemplate' => "<a href='javascript:void(0)'>{label}</a>",
'activateParents' => true,
])
?>
</div
Whit my 'linkTemplate' I am disabling all the links which obviously is not my goal :) ( It should disable the parents only ). Alsom, with what token ( or some other option, because I think items is the only token for the children ) I should append the child item label on this line : \n<h3 class='menu-title'>{label}</h3>. In my case what I get echoed is just {label} like a string.
Many thanks!

Related

TinyMCE CakePHP 3.0+ not selecting textarea (nothing happens)

CakePHP seems to be loading the plugin because I'm not getting any errors when loading from bootstrap, including the helper in the control and then using it in the view, but nothing happens to my <textarea></textarea> tags which I placed before and after the script in the view. I have also loaded jQuery. Any ideas on what might be wrong?
Controller:
public $helpers = ['TinyMCE.TinyMCE'];
View:
<textarea></textarea>
$this->TinyMCE->editor(array('theme' => 'advanced'));
<textarea></textarea>
HTML (view source, when the page is loaded):
<textarea>
</textarea>
<script>
//<![CDATA[
tinymce.init({
script : "/TinyMCE/js/tiny_mce/tiny_mce.js",
load_script : "1",
theme : "advanced"
});
//]]>
</script>
<textarea>
</textarea>
Bootstrap:
Plugin::load('TinyMCE', ['autoload' => true]);
Configure::write('TinyMCE.configs', array(
'advanced' => array(
'mode' => 'textareas',
'theme' => 'advanced')));
I also just noticed, when I pr($this->TinyMCE);
I get:
TinyMCE\View\Helper\TinyMCEHelper Object
(
[helpers] => Array
(
[0] => Html
)
[theme] =>
[plugin] =>
[fieldset] => Array
(
)
[tags] => Array
(
)
[implementedEvents] => Array
(
[View.beforeRender] => beforeRender
)
[_config] => Array
(
)
)
For some reason there is nothing in [theme] value, it should be - advanced. What am I doing wrong? Is something wrong with this line:
$this->TinyMCE->editor(array('theme' => 'advanced'));
Helper TinyMCE for CakePHP 2 with preset function.
TinyMCE
Download TinyMCE : http://www.tinymce.com/download/download.php
Copy /tinymce/jscripts/ tiny_mce folder to /app/webroot/js (like : /app/webroot/js/tiny_mce).
Controller
public $helpers = array('Tinymce');
Behavior
$this->Tinymce->input($Model.fieldName, $options = array(), $tinyoptions = array(), $preset = null)
Example
<div class="posts form">
<?php echo $this->Form->create('Post');?>
<fieldset>
<legend><?php echo __('Add Post'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Tinymce->input('Post.content', array(
'label' => 'Content'
),array(
'language'=>'en'
),
'bbcode'
);
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
http://bakery.cakephp.org/2012/04/11/Helper-TinyMCE-for-CakePHP-2.html#

Yii2 remember sort option after submit the searchForm

I use an ActiveForm in Yii2 for my SearchModel. After click on search button the form fields remember previous values but SorterDropdown is refreshed.
<?php echo SorterDropdown::widget(['sort' => $dataProvider->sort,
'label' => $model->sortedBy($dataProvider->sort->attributes),])
?>
SorterDropdown is just a wrapper of ButtonDropdown.
How can I forse the SorterDropdown to remember sort order (and show it) after the form submition?
class SorterDropdown extends LinkSorter
{
public $label;
protected function renderSortLinks()
{
$attributes = empty($this->attributes) ? array_keys($this->sort->attributes) : $this->attributes;
$links = [];
foreach ($attributes as $name) {
$links[] = Html::tag('li', $this->sort->link($name, ['tabindex' => '-1']));
}
if (empty($this->label))
$this->label = 'Sort';
return \yii\bootstrap\ButtonDropdown::widget([
'encodeLabel' => false,
'label' => $this->label,
'dropdown' => [
'items' => $links,
],
]);
}
You should add some class to li element, which indicate active state of it and special style in CSS file.

Yii2 Modal Dialog on Gridview update button not load the update form

With reference to How to implement Yii2 Modal Dialog on Gridview view and update button?. I did my code below, but it popups a window with no update form. could anyone please let me know the correct code? (i use Kartik Gridview)
This is the code for column:
['class' => '\kartik\grid\ActionColumn',
'template'=>'{update}{delete}{view}',//{view}'//{view}{delete}',
'headerOptions' => ['width' => '20%', 'class' => 'activity- update-link',],
'contentOptions' => ['class' => 'padding-left-5px'],
'buttons' => [
'update' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>','/projects/update?id='.$key.'', [
'class' => 'activity-update-link',
'title' => Yii::t('yii', 'Project Update'),
'data-toggle' => 'modal',
'data-target' => '#activity-modal',
'data-id' => $key,
'data-pjax' => '0',
]);
},
],
],
This is JS code:
<?php $this->registerJs(
'
function init_click_handlers(){
$(".activity-update-link").click(function(e) {
var fID = $(this).closest("tr").data("key");
$.get(
"update",
{
id: fID
},
function (data)
{
$("#activity-modal").find(".modal-body").html(data);
$(".modal-body").html(data);
$("#activity-modal").modal("show");
}
);
});
}
init_click_handlers(); //first run
$("#project_pjax_id").on("pjax:success", function() {
init_click_handlers(); //reactivate links in grid after pjax update
});
');
?>
<?php
Modal::begin([
'id' => 'activity-modal',
'header' => '<h4 class="modal-title">Update Project Testing</h4>',
'size'=>'modal-lg',
'footer' => '<a href="#" class="btn btn-primary" data-
dismiss="modal">Close</a>',
]); ?>
<div class='well'></div>
<?php Modal::end(); ?>
Thanks all for watching my question. I solved my issue by adding modal into main.js file of the assets folder using class instead of ID.

cakephp 3: change class input error

I build my form template according the documentation. It seemed everything was fine until I get fields errors. Now I have two problems:
How can I change the class name of the forms fields when they get error?
Solution:
$this->loadHelper('Form', [
'templates' => 'your_template_file',
'errorClass' => 'your-class',
]);
How can I set escape => false in the error-message from cakephp, when the field get error? Because I have icon within that div, such as
<div class="error-message"><i class="fa fa-times"></i> My error</div>
Well, I got part of th solution. To escape HTML I could put $this->Form->error('field', null, ['escape' => false]); in all fields, but it´s a hard manually task. I´d like to keep escape with default of all fields errors. I could edit the FormHelper.php class. However, I think that is not good idea.
My form template is:
'formStart' => '<form {{attrs}} class="form-horizontal" novalidate>',
'inputContainer' => '{{content}}',
'input' => '<input type="{{type}}" name="{{name}}" {{attrs}} class="form-control"/>',
'checkbox' => '<input type="checkbox" value="{{value}}" name="{{name}}" {{attrs}}/>',
'textareaContainerError' => '{{content}}',
'textarea' => '<textarea name="{{name}}" {{attrs}} class="form-control"></textarea>',
'select' => '<select name="{{name}}" {{attrs}} class="form-control">{{content}}</select>',
'button' => '<button {{attrs}} class="btn btn-primary">{{text}}</button>',
'nestingLabel' => '{{input}}',
'formGroup' => '{{input}}',
to the second part of the question: you can extend FormHelper like in code below, so that escape will be set to false by default
// extended FormHelper, this goes in src/View/Helper
namespace App\View\Helper;
use Cake\View\Helper;
class MyFormHelper extends Helper\FormHelper
{
public function error($field, $text = null, array $options = [])
{
if (!isset($options['escape'])) {
$options['escape'] = false;
}
return parent::error($field, $text, $options);
}
}
next create alias for this helper in AppController.php
public $helpers = [
'Form' => ['className' => 'MyForm']
];
this also allows you to add more customization of your own and at any time, you can go back to default implementation of FormHelper, just remove that alias from AppController.php.
For those who wants an 'easy solution' to escape error message on some fields, you cant simply set escape options to false :
<?= $this->Form->input('email', [
"label" => "Email",
"error" => [
"escape" => false
]
]) ?>

zend framework - group elements of a form within a subforms [duplicate]

I would like to be able to add a hidden form field using array notation to my form. I can do this with HTML like this:
<input type="hidden" name="contacts[]" value="123" />
<input type="hidden" name="contacts[]" value="456" />
When the form gets submitted, the $_POST array will contain the hidden element values grouped as an array:
array(
'contacts' => array(
0 => '123'
1 => '456'
)
)
I can add a hidden element to my form, and specify array notation like this:
$form->addElement('hidden', 'contacts', array('isArray' => true));
Now if I populate that element with an array, I expect that it should store the values as an array, and render the elements as the HTML shown above:
$form->populate($_POST);
However, this does not work. There may be a bug in the version of Zend Framework that I am using. Am I doing this right? What should I do differently? How can I achieve the outcome above? I am willing to create a custom form element if I have to. Just let me know what I need to do.
You have to use subforms to get the result you seek. The documentation was quite a ride but you can find it here
Using what I found there I constructed the following formL
<?php
class Form_Test extends Zend_Form {
public function init() {
$this->setMethod('post');
$this->setIsArray(true);
$this->setSubFormDecorators(array(
'FormElements',
'Fieldset'
));
$subForm = new Zend_Form(array('disableLoadDefaultDecorators' => true));
$subForm->setDecorators(array(
'FormElements',
));
$subForm->addElement('hidden', 'contacts', array(
'isArray' => true,
'value' => '237',
'decorators' => Array(
'ViewHelper',
),
));
$subForm2 = new Zend_Form(array('disableLoadDefaultDecorators' => true));
$subForm2->setDecorators(array(
'FormElements',
));
$subForm2->addElement('hidden', 'contacts', array(
'isArray' => true,
'value' => '456', 'decorators' => Array(
'ViewHelper',
),
));
$this->addSubForm($subForm, 'subform');
$this->addSubForm($subForm2, 'subform2');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setValue('Submit');
$this->addElement('submit', 'submit');
}
}
Wich outputs this html:
<form enctype="application/x-www-form-urlencoded" method="post" action=""><dl class="zend_form">
<input type="hidden" name="contacts[]" value="237" id="contacts">
<input type="hidden" name="contacts[]" value="456" id="contacts">
<dt id="submit-label"> </dt><dd id="submit-element">
<input type="submit" name="submit" id="submit" value="submit"></dd></dl></form>
And when submited the post looks like:
array(2) {
["contacts"] => array(2) {
[0] => string(3) "237"
[1] => string(3) "456"
}
["submit"] => string(6) "submit"
}
So thats how you can create the kind of forms you seek. Hope this helps! if you have a question post a comment!
Its quite hackish if you ask me. You basically create subforms but disable there form decorators so just the element gets output. Since the identical contacts[] elements are in different form object zend does'nt overwrite them and it works. But yeah..
Edit: changed it a bit to remove labels and garbage arount the hidden inputs.
To use array notation, you need to specify that the element "belongs to" a parent array:
$form->addElement('hidden', 'contact123', array('belongsTo' => 'contacts', 'value' => '123'));
$form->addElement('hidden', 'contact456', array('belongsTo' => 'contacts', 'value' => '456'));
This indeed seems to be a bug in Zend Framework - the value attribute for an element is properly set to array, but it's ignored when the element renders - it just uses$this->view->escape($value) to output element's html.
I've solved this by implementing a custom helper for such elements:
class My_View_Helper_HiddenArray extends Zend_View_Helper_FormHidden
{
public function hiddenArray($name, $value = null, array $attribs = null)
{
if (is_array($value)) {
$elementXHTML = '';
// do not give element an id due to the possibility of multiple values
if (isset($attribs) && is_array($attribs) && array_key_exists('id', $attribs)) {
unset($attribs['id']);
}
foreach ($value as $item) {
$elementXHTML .= $this->_hidden($name, $item, $attribs);
}
return $elementXHTML;
} else {
return $this->formHidden($name, $value, $attribs);
}
}
}
Which, when used the next way:
$contacts = $form->createElement('hidden', 'contacts')
->setIsArray(true)
->setDecorators(array(
array('ViewHelper', array('helper' => 'HiddenArray')),
));
$form->addElement($contacts);
generates the needed output.
The reason to extend Zend_View_Helper_FormHidden here is just to be able to call the default behaviour if no array value is set ( return parent::formHidden($name, $value, $attribs) ).
Hope this helps someone :)
For the newer versions of ZF you should use https://framework.zend.com/manual/2.1/en/modules/zend.form.elements.html#multicheckbox