Hi,
I tried to use computed_field in my vocabulary "artists". A user adds a term like "family name, first name". The term has two fields "family name" and "first name" which should be filled automatically.
For the family name I tried this:
Computed Code (PHP)
$wert = field_get_items($entity_type, $entity, 'name');
$arr = preg_split('/\s*\,\s*/', $wert);
var_dump($arr);
$entity_field[0]['value'] = $arr[0];
Display Code (PHP)
$display_output = $entity_field_item['value'];
I made a test with "Zuckerberg, Marc" and got only "Z" as value of field_family_name and the following error:
Notice: String offset cast occurred in field_get_items() (line 927 von /var/www/myserver/httpdocs/mysite/modules/field/field.module).
What's my fault? Please help me...
Comments
Comment #2
gonzaguer CreditAttribution: gonzaguer commentedHi,
I'm not sure what's your looking at but according to drupal api: field_get_items returns an array of field items keyed by delta if available, FALSE otherwise.
So your preg_split can't work.
Then preg_split('/\\s*\\,\\s*/', 'Zuckerberg, Marc', -1); should return :
array (
0 => 'Zuckerberg',
1 => 'Marc',
)
Ind the end you can do your thing : $entity_field[0]['value'] = $arr[0];
Hope this will help
Comment #3
zwerg CreditAttribution: zwerg commentedHi gonzaguer,
thanks for your comment. I want to get the taxonomy term name by computed field. Because it is separated with comma between family name und first name I need to get these values by computed field. So I tried using
preg_split
.I'd test with
explode()
instead ofpreg_split
but that also won't work.Is there any posibility to get the value of taxonomy term name and split it at the comma?
Thanks in advance!
Comment #4
gonzaguer CreditAttribution: gonzaguer as a volunteer commentedI meant that you should get an array from field_get_items.
try : dpm($wert, 'wert'); to display the debug
You should be getting an array. put here the result of the dpm
We'll check it together
Comment #5
zwerg CreditAttribution: zwerg commentedI get these information:
wert =>
Notice: Undefined variable: wert in include() (line 284 von /httpdocs/mysite/sites/all/themes/bootstrap_business/page.tpl.php).
array(1) { [0]=> string(0) "" }
and using devel I'll get these information:
and when I have a look at the node where the term is used there are some errors:
Notice: Undefined variable: term in eval() (line 1 von /site/sites/all/modules/computed_field/computed_field.module(394) : eval()'d code).
Notice: Trying to get property of non-object in eval() (line 1 von /site/sites/all/modules/computed_field/computed_field.module(394) : eval()'d code).
Notice: Undefined offset: 1 in eval() (line 5 von /site/sites/all/modules/computed_field/computed_field.module(394) : eval()'d code).
Comment #6
gonzaguer CreditAttribution: gonzaguer as a volunteer commentedWeird!
When you say you'r using devel to get informations. Is it from the devel tab on node/*?
If not, could you click on the devel tab on node/* to see the field data from field_familyname.
It seems that you'r not collecting the whole value from your content type form field.
Comment #7
zwerg CreditAttribution: zwerg commentedDevel shows the value "Z"
Comment #8
gonzaguer CreditAttribution: gonzaguer as a volunteer commentedOh i see.
Do this :
$wert = field_get_items($entity_type, $entity, 'name');
$arr = preg_split('/\s*\,\s*/', $wert[0]['value']);
$entity_field[0]['value'] = $arr[0];
It was because field_get_items returns an array like
array(0 => array('value' => 'Zuckerberg, Marc'))
Comment #9
zwerg CreditAttribution: zwerg commentedThanks for your reply. Using this code I'll get the following error:
And the value of field_familyname is still "Z"...