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

zwerg created an issue.

gonzaguer’s picture

Hi,

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

zwerg’s picture

Hi 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 of preg_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!

gonzaguer’s picture

I 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

zwerg’s picture

try : dpm($wert, 'wert'); to display the debug

I 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:

field_familyname (Array, 1 element)
    and (Array, 1 element)
        0 (Array, 1 element)
            value (String, 1 characters ) Z

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).

gonzaguer’s picture

Weird!
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.

zwerg’s picture

Devel shows the value "Z"

gonzaguer’s picture

Oh 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'))

zwerg’s picture

Thanks for your reply. Using this code I'll get the following error:

Notice: String offset cast occurred in field_get_items() (line 927 von httpdocs/mysite/modules/field/field.module).
Warning: Illegal string offset 'value' in eval() (line 2 von httpdocs/mysite/sites/all/modules/computed_field/computed_field.module(394) : eval()'d code).

And the value of field_familyname is still "Z"...