tntnOdln
(Tonton Odilon)
1
Hi 
My table contient some fields with camel2words like : priceItem
On my label this is like as : Price Item
I would change this by Price<br>Item : single line break
Can I ?
On Yii [color="#FF0000"]2.0[/color] I tried with BaseInflector.php [… mysite\vendor\yiisoft\yii2\helpers]
I changed ’ ', by ‘<br>’,
public static function camel2words($name, $ucwords = true)
{
$label = trim(strtolower(str_replace([
'-',
'_',
'.',
], '<br>', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($label) : $label;
}
and
public static function humanize($word, $ucAll = false)
{
$word = str_replace('_', '<br>', preg_replace('/_id$/', '', $word));
return $ucAll ? ucwords($word) : ucfirst($word);
}
but there aren’t any change 
always it is like this : Price Item
Thanks
tri
(tri - Tommy Riboe)
2
While I’m trying to understand the search pattern
you may try change the space before the replacement
preg_replace(’/(?<![A-Z])[A-Z]/’, ’ \0’, $name)
(It’s not recommended to make changes to the framework code. Can’t tell exactly how to override.)
tri
(tri - Tommy Riboe)
3
camel2words() is called from several places. The result may not be what you expected.
You may try overiding generateAttributeLabel
http://www.yiiframework.com/doc-2.0/yii-base-model.html#generateAttributeLabel()-detail
tntnOdln
(Tonton Odilon)
4
Thank Tri,
My goal : the labels are in two lines
So I don’t know how can I :
I thought to replace whitespace [ ] by single line break [<br>]
So I tried :
public static function camel2words($name, $ucwords = true)
{
/*
$label = trim(strtolower(str_replace([
'-',
'_',
'.',
], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
*/
$label = trim(strtolower(str_replace([
'-',
'_',
'.',
], '<br>', $name)));
return $ucwords ? ucwords($label) : $label;
}
and
public static function humanize($word, $ucAll = false)
{
/*
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
*/
$word = str_replace('_', '<br>', $word)
return $ucAll ? ucwords($word) : ucfirst($word);
}
but it doesn’t work.
You say :
So do you have another idea to put the label on two lines, instead of a single line ?
OK I’m going to search other place on api…
tri
(tri - Tommy Riboe)
5
Did you try inserting <br> where I suggested (in boldface)?. Worth a temp try.
And look for generateAttributeLabel() here
http://www.yiiframework.com/doc-2.0/yii-base-model.html
Should be possible to override in any kind of model e.g an ActiveRecord.
Example from a Yii 1.x AR model, may be same for 2.0 (still learning)
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'UserId' => 'User',
// ...
'LastLogin' => 'Last Login',
);
}
tri
(tri - Tommy Riboe)
6
tntnOdln
(Tonton Odilon)
7
Hi Tri
Yes it is : 
in models\product.php
I can put [<br />]
public function attributeLabels()
{
return [
'id' => 'ID',
'priceItem' => 'Price<br />Item',
'titleItem' => 'Title<br> Item',
but <br> (or <br />) does not function like code html so I have not it
I see this :
±---------------------+
| Price<br/>Item |
±---------------------+
instead of :
±--------+
| Pric e |
| It e m |
±--------+
So I must find another solution
tntnOdln
(Tonton Odilon)
8
I found the solution with CSS
.grid-view th {
white-space: normal!important;
}
thanks