Change home text and URL in Yii2 Breadcrumbs widget - php

I want to change default text "Home" and also the URL for home.
While searching on it I found suggestion that need to set homeUrl but I don't have any idea where to set it.
Please ask for code if you need it.

You need to configure $homeLink property:
echo Breadcrumbs::widget([
'homeLink' => [
'label' => 'My name',
'url' => '/my/url',
],
'links' => [
// your items
],
// rest of widget config
]);

Related

set default value using Kartik selectbox yii2

Please help... I am trying to set default value for this: I know it is based on Kartik select on yii2. I didn't use it before. Here is my source code I would need to set default value based on $_GET parameters. But problem is I can't set any. The source is this...
<?php
echo $form->field($profile, 'country_id')->widget(Select2::classname(), [
'language' => Yii::$app->language,
'data' => ArrayHelper::map($countries, 'id', 'title_' . mb_substr(Yii::$app->language, 0, 2)),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'id' => 'country-select',
'placeholder' => Yii::t('frontend', 'Select a country')],
'pluginOptions' => [
'allowClear' => true,
],
])->label($Country, ['class' => 'label-class'])
?>
Where Should I set it in this case. Sorry, I just saw this plugin at first time...
if you wanna set the default value, you should use the model like the following code;
$profile->country_id = isset($profile->country_id) ? $profile->country_id : 1 // like that
also you can use in afterFind function on the model.
check this Yii2: How to set default attribute values in ActiveRecord? and this https://www.yiiframework.com/doc/guide/2.0/en/tutorial-core-validators#default

Yii2 Kartik Grid View Export Config

Summary: Trying to add export in the for csv & pdf download.
Followed documantation to install. Grid view is otherwise working.
Also added as module in config/web.php's $config array -
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
//'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '#kvgrid/messages',
//'forceTranslation' => false
]
]
],
N.B: I am using basic template and new in yii2. I have tried other fixes like composer update etc as suggested in various posts but really stuck with the problem.
the thing causing problem is - Yii::t('kvgrid', 'Reset Grid')
Can somene give me a direction here. I guess it is very simple issue :(
You was got above error because of you have not configure i18n component in web.php file.
You need to configure i18n component like as,
'i18n' => [
'translations' => [
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
],
]
],
Or uncomment the gridview modules's i18n configuration
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#kvgrid/messages',
'forceTranslation' => true
]
]
You have to replace
Yii::t('kvgrid', 'Reset Grid')
to
Yii::t('app', 'Reset Grid')
in your view file
I suggest better to generate CRUD using the Ajax Crud Generator, it will do all the required task for CRUD and export as well...try this

Yii2: How can I place something into a Breadcrumbs widget?

I find the Breadcrumbs widget quite useful. However, on the right side within the widget there is enough space for something else. If I'd like to put a link ('a' tag, but could be actually any other small thing) right aligned into the Breadcrumbs, how could I do that? What is a simple and proper solution? Should I extend the class, develop my own, use begin and end of the widget somehow?
If you look at yii\widgets\Breadcrumbs you see that there is a third parameter in the breadcrumbs items
From the Yii2 file
[
'label' => 'Post Category',
'url' => ['post-category/view', 'id' => 10],
'template' => "<li><b>{link}</b></li>\n", // template for this link only
],
So by adding something like this in your main layout file
$this->params['breadcrumbs'][] = [
'label' => 'Your Label',
'url' => ['controller/action'],
'template' => "<li style="float: right;">{link}</li>\n"
];
you will get a link at the right side. This link will come with a / prefixed, but you can bind it to a .class instead and configure it the way you want.
'template' => "<li class=\"yourClass\">{link}</li>\n"

Override Yii2 assetManager config in controller

I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.

ZF2 Input Filter Change Message Color

I made typical form with ZF2 form and wanted to add validation using ZF2 InputFilter. It was success but the colour of error message is black which is looks strange. I tried to change the colour by using method I've searched like this:
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => '<div style="color:red;">Please enter User Name!</div>'
),
),
),
But, instead of message's colour changed to red, it showed the tag with style, in other word, just plain HTML. What is the proper way to achieve my need?
Easiest way would be to modify the view helper ;)
Inside your module.config.php
'view_helpers' => [
'factories' => [
'formelementerrors' => function($vhm) {
$fee = new \Zend\Form\View\Helper\FormElementErrors();
$fee->setAttributes([
'class' => 'your error classes'
]);
return $fee;
}
]
]
The alternate approach when rendering errors using $this->formElementErrors() would be to add the error classes inside the ViewHelper directly
$this->formElementErrors($element, ['class' => 'my error classes']);

Categories