Hi, I use a PHP field for print the first image of the body.

I write this code:

<?php

$vid = db_query("SELECT vid FROM {node} WHERE nid=:nid", array(':nid'=>$data->nid) )->fetchField();
$node = node_load($data->nid,$vid,TRUE);

if( $imgs = explode('[img', $node->body['es']['0']['value']) ){
	$cont=1;
	while( !$imagen and $cont<sizeof($imgs) ){
		$partes = explode('[/img]', $imgs[$cont]);
		$principio = explode(']', $partes[0]);
		if( !(strstr($principio[1], 'smilies')) )
			$imagen = $principio[1];
		else $cont++;
	}
}
if( isset($imagen) )
	print $imagen;
?>

The code works but give this errors:

* Notice: Undefined variable: imagen en __lambda_func() (línea 8 de C:\xampp\htdocs\drupal7\sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(196) : runtime-created function).
* Notice: Undefined variable: imagen en __lambda_func() (línea 8 de C:\xampp\htdocs\drupal7\sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(196) : runtime-created function).

Comments

meatbag’s picture

I try to use the following code

value code
return $data->_field_data['nid']['entity']->field_name['und'][0]['value'];
output code
<?php print $value;?>

But i get the following errors:
# Notice: Trying to get property of non-object in __lambda_func() (line 1 of D:\xampplite\htdocs\drupal\sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(132) : runtime-created function).
# Notice: Undefined property: stdClass::$_field_data in __lambda_func() (line 1 of D:\xampplite\htdocs\drupal\sites\all\modules\views_php\plugins\views\views_php_handler_field.inc(132) : runtime-created function).

casey’s picture

#TS:
You check for a variable that isn't defined yet in the while condition.

while( !$imagen and $cont<sizeof($imgs) ){

Should be

while( !isset($imagen) and $cont<sizeof($imgs) ){
casey’s picture

#1
What do you get if you use the following output code:

<?php
print_r($data);
?>
meatbag’s picture

If i use the following code

value code
none
output code
<?php print $data->_field_data['nid']['entity']->field_name['und'][0]['value'];?>

instead of

value code
return $data->_field_data['nid']['entity']->field_name['und'][0]['value'];
output code
<?php print $value;?>

I can get the correct result.


Here's the $data output

stdClass Object ( [feeds_item_url] => [node_title] => notitle [nid] => 204 [node_feeds_item_title] => [node_feeds_item_nid] => 203 [node_created] => 1388534400 [field_data_field_name_node_entity_type] => node [field_data_field_imgurl_node_entity_type] => node [field_data_field_image_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 204 [uid] => 0 [title] => notitle [log] => Created by FeedsNodeProcessor [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 204 [type] => items [language] => und [created] => 1388534400 [changed] => 1297999470 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1297999470 [revision_uid] => 1 [field_image] => Array ( ) [field_imgurl] => Array ( ) [field_name] => Array ( [und] => Array ( [0] => Array ( [value] => test [format] => plain_text [safe_value] => test ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1297906597 [last_comment_name] => [last_comment_uid] => 0 [comment_count] => 0 [name] => [picture] => 0 [data] => ) ) ) )
casey’s picture

Ow wait a sec... the field data is of course not yet added tot the result set when the value code is run. I'll see what I can do.

marta_yo’s picture

#2 works!!
thank you very much

casey’s picture

Status: Active » Fixed

For field data keep an eye on #1061388: Add field data to the result set.

Status: Fixed » Closed (fixed)
Issue tags: -undefined variable

Automatically closed -- issue fixed for 2 weeks with no activity.