[Drupal] PHP array is printing array instead of value

Hi all

I have a small snippet of PHP below which for some reason is printing array instead of the value.
Can anybody see what I’m doing wrong?

The first snippet prints ok, its the second one(field_event_location) thats printing array.

$build['node_'.$node->nid]['title'] = array(
      '#type'=> 'markup',
      '#markup'=> l($node->title,drupal_get_path_alias('node/'.$node->nid)),
      '#weight'=> 0,
    );

$build['node_'.$node->nid]['field_event_location'] = array(
      '#type'=> 'markup',
      '#markup'=> $node->field_event_location,
      '#prefix'=> '<p>',
      '#suffix'=> '</p>',
      '#weight'=> 1,
    );

Thanks, cb

hi What do you use to echo the value?

when u use after a variable name it will automatically create an array.

Peanuts

Thanks, and good question.
This is taken from another post I’ve been working on for Drupal CMS.
Tell you the truth I’m not to sure myself (:

The code above is wrapped inside a function and then added to a block which displays the results.

$block['content'] = eventlist_block_eventlist();
function eventlist_block_eventlist() {

.....

$build['node_'.$node->nid]['field_event_location'] = array(
      '#type'=> 'markup',
      '#markup'=> $node->field_event_location,
      '#prefix'=> '<p>',
      '#suffix'=> '</p>',
      '#weight'=> 1,
    );
}

You can see the full script here if that helps http://www.sitepoint.com/forums/showthread.php?1199612-Entity-Field-Query-and-Module

Cheers peanuts

If it’s printing “Array”, you’re not going deep enough into the multi-dimensional array

For example, the value of
$build['node_123']['field_event_location']['#type']
is 3 deep.

Thanks Mittineague

If I change from

'#markup'=> $node->field_event_location,

to

'#markup'=> $node->nid,
//or
'#markup'=> $node->title,

It works. Array is then removed and replaced with the id number/title of that node.
Though I still have the below code above it.

$build['node_'.$node->nid]['field_event_location'] = array(

It must be to do with the field name.
I’ll have to investigate further unless you can see the problem.

Barry

This also works

$build['node_'.$node->nid][] = array(
      '#type'=> 'markup',
      '#markup'=> $node->title,
      '#prefix'=> '<p>',
      '#suffix'=> '</p>',
      '#weight'=> 1,
    );

Can anybody help with this?

I’m also running the Devel module which funny enough shows the correct value of field_event_location.

Just can’t figure out what I’m doing wrong or how to dig deeper into this as Mittineague mentioned?

$build['node_'.$node->nid]['field_event_location'] = array(
      '#type'=> 'markup',
      '#markup'=> $node->field_event_location,
      '#prefix'=> '<p>',
      '#suffix'=> '</p>',
      '#weight'=> 1,
    );

Cheers, cb

You’re not getting syntax errors from the extra comma at the end of the array? i.e.

$array = ( ‘1’ => ‘1’, ‘2’ => ‘2’, );
should give some kind of “expected” message no?

Thanks Mittineague

I was just updating the post as you posted, if this helps which was taken from the devel Module output.

node_487
	field_event_location
		#type
		#markup
			und(Array, 1 element)
				0(Array, 3 elements)
					value(String, 9 characters) Location1
					format(NULL)
					safe_value(String, 9 characters) Location1
&#8230;
node_480
	field_event_location
		#type
		#markup
			und(Array, 1 element)
				0(Array, 3 elements)
					value(String, 9 characters) Location2
					format(NULL)
					safe_value(String, 9 characters) Location2

Cheers, cb

update:
No errors, it just prints Array still?

If I remove the comma it says:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ‘)’ in…

If this helps?

The exact path to the value inside field_event_location as above:
$…[‘elements’][‘node_480’][‘field_event_location’][‘#markup’][0][‘value’]

That’s because it is yet another array. “und” looks a bit odd, “and”?
And if that doesn’t make it deep enough it’s an array containing a single array (Cheesh!)

So you need something like

$build['node_'.$node->nid]['field_event_location'] = array(
      '#type'=> 'markup',
      '#markup'=> $node->field_event_location['#markup'][0]['safe_value'],
      '#prefix'=> '<p>',
      '#suffix'=> '</p>',
      '#weight'=> 1,
    );  

(or just value depending)

“und” looks a bit odd

I know, still trying to figure out where this fits in, something Drupal adds.

ERROR
Notice: Undefined index: #markup in eventlist_block_eventlist() (line 82 of…

I’ve tried both which give the same error

'#markup'=> $node->field_event_location['#markup'][0]['value'],
'#markup'=> $node->field_event_location['#markup'][0]['safe_value'],

Devel is now showing:

#markup (NULL)
$...['elements']['node_487']['field_event_location']['#markup']

? (:

I just searched for “devel Module und” and guess what, another level!
Try

'#markup'=> $node->field_event_location['#markup']['und'][0]['safe_value'],

and

'#markup'=> $node->field_event_location['#markup']->und[0]['safe_value'],

I just searched for “devel Module und” and guess what, another level!

:lol: that made me laugh :cool:

I’ve been at this for well over a week (:

Nothing has changed. Everything still NULL as before.
I did try the second option but gives an error referring to non-object though I think the first way is the prefered way after much reading.

I’m now wondering if the first line has anything to do with it?
As I said in an early post, I can remove the reference to [‘field_event_location’] altogether and it was still working.

$build['node_'.$node->nid]['field_event_location'] = array(

Start a fresh tomorrow unless your still online, and thanks for your time Mittineague.

Barry

I just searched for “devel Module und”

It got me thinking and I done a search myself earlier :smiley:

'#markup'=> $node->field_event_location['und'][0]['value'],

We just needed to remove [‘#markup’]
I can’t believe how long I’ve been working on this, though got there in the end Mittineague, feels great :cool:

But already, a few other problems have become exposed. If you get chance, might start another thread if needed.


Ok, I’ve cloned the location snippet above and will be using the date of the event to do further querying.
As you can see below, the date condition is set between two dates.

How do I replace the first date 2014-03-12 with NOW() so I don’t have to keep editing the file?

->fieldCondition('field_event_date_and_time', 'value', array('2014-03-12', '2014-03-31'), 'BETWEEN')

I thought something like this though gives errors.

->fieldCondition('field_event_date_and_time', 'value', array(NOW(), '2014-03-31'), 'BETWEEN')

Thanks again, Barry