data-* attributes do not work with Html::a() in Yii 2 - yii2

I have the following:
Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // DOES NOT WORK
'data' => [
'id' => 123, // DOES NOT WORK
],
]);
As per docs, both of the specified data-* attributes in Html::a helper should render their respective attributes in the HTML output, but they do not, and I do not understand why.
Yii 2 documentation on renderTagAttributes also states the following:
Renders the HTML tag attributes.
Attributes whose values are of boolean type will be treated as boolean
attributes.
Attributes whose values are null will not be rendered.
The values of attributes will be HTML-encoded using encode().
The "data" attribute is specially handled when it is receiving an
array value. In this case, the array will be "expanded" and a list
data attributes will be rendered. For example, if 'data' => ['id' =>
1, 'name' => 'yii'], then this will be rendered: data-id="1"
data-name="yii". Additionally 'data' => ['params' => ['id' => 1,
'name' => 'yii'], 'status' => 'ok'] will be rendered as:
data-params='{"id":1,"name":"yii"}' data-status="ok".
EDIT: I am trying to do this inside GridView column.

Okay, since I have used Html::a inside a GridView column, you will have to change the output format of that column. html will not work for data attributes, so you will need to switch to raw:
[
'label' => 'Actions',
'format' => 'raw',
'value' => function($model) {
return Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // WORKS
'data' => [
'id-second' => 123, // WORKS
],
]);
},
]

Related

How to display values according to it's name in DetailView yii2?

I'm trying to display category values according to it's name, not id. I'm trying to do like so:
[
'attribute' => 'category_id',
'value' => 'category.name',
],
But then attribute doesn't get displayed. It displays: name instead of Category. Category name is displayed correctly.
Also tried 'category.name' ,but it's displaying same values, and 'category_id' is displaying label correctly, but the name - according to id's.
How should I solve that?
Are you define relation for category, like this?
public function getCategory()
{
return $this->hasOne(Category::className(),['id'=>'category_id']);
}
If you want to display in GridView, you can do it like you
[
'attribute' => 'category_id',
'value' => 'category.name',
],
If you want to display in DetailView, you can do it like this
[
'attribute' => 'category_id',
'value' => $model->category ? $model->category->name : '',
],
seems you need a label.
the simplest way is add the label direcly in the detail view item
[
'label' => 'Category',
'attribute' => 'category_id',
'value' => ....,
],

yii2 SluggableBehavior is applied to only emty slug field

I am using SluggableBehavior in such way
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'slugAttribute' => 'alias',
],
];
}
It works nice If field 'alias' in form is empty.
How to ignore this behaviors If field alias is not empty on form submitting ?
thanks in advance !
Add 'immutable'=>true in the behavour config.
The behaviour works in such way, that if the slugAttribute is not empty when immutable is on, that attribute will not be changed.
Try something like:
Configure this behavior with specific name:
return [
'slug' => [
'class' => SluggableBehavior::className(),
'attribute' => 'name',
'slugAttribute' => 'alias',
],
];
In controller load attributes from the form (before validation).
Check if alias attribute is empty.
If it is not - detach this behavior ($this->detachBehavior('slug')).
Validate model.

Yii2 kartik detailview hide label

If I'm doing 'label' => false, it is still showing the empty cell of the label. Is it possible completely not to render label cell somehow? Many thanks!
UPDATE: 'labelColOptions' => ['hidden' => true] works!
You can use visible property :
For example,
[
'attribute' => 'name',
'value' => whatever,
'visible' => (!empty($model->name)),
]

Multiple categories in Url

I want to create links something like that:
http://example.com/cat1/itemname-1
http://example.com/cat1/cat2/itemname-2
http://example.com/cat1/cat2/cat3/itemname-3
http://example.com/cat1/cat2/cat3/[..]/cat9/itemname-9
How rule looks like in yii2 UrlManager and how to create links for this?
Url::to([
'param1' => 'cat1',
'param2' => 'cat2',
'param3' => 'cat3',
'slug' => 'itemname',
'id' => 3
]);
Above code is really bad for multiple category params.
I add that important is only last param it means ID.
Controller looks like that:
public function actionProduct($id)
{
echo $id;
}
The below url rule would to this trick but you have to build the "slug" with the categories within your controller:
'rules' => [
['route' => 'module/controller/product', 'pattern' => '<slug:(.*)+>/<id:\d+>', 'encodeParams' => false],
]
Generate the Url:
yii\helpers\Url::toRoute(['/module/controller/product', 'slug' => 'cat1/cat2/cat3', 'id' => 1])
The output would be:
example.com/cat1/cat2/cat3/1

Multiple values in one field Yii2

How to display multpiple values in one field.. I Use Select2 Widget. If i use $courses_model[0] it display only one value
Controller
public function actionUpdateteachers($id)
{
$courses_model = ReferenceTeachersCourses::find()->where(['reference_teachers_id' => $id])->all();
.....
}
View
...
<?= $form->field($courses_model[0], 'reference_course_type_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map($courses,'id','name'),
'options' => ['multiple' => true],
'pluginOptions' => [
'allowClear' => true,
],
]);
...
?>
This widgets works fine - but your $courses_model[0]->reference_course_type_id must have an array of ids as a value if you want to see multiple values selected.