I'm trying to create a custom user profile in Drupal 7. One of the fields references the user object, and I can't figure out what the syntax is to do that. I've read that '->' should be used, but I'm not sure exactly how.
The structure of a print_r($user_object) is at: http://pastebin.mozilla.org/1741565
I'm trying to get to the data inside field_country[data] - just not sure how to do it.
In addition, when I try to access $user_object[user_relationships_ui], nothing is visible. It doesn't throw an error, but doesn't print anything, either.
Thanks for the help
The way I figured out how to do it was to cast the object as an array -
$user_object= (array) $user_profile['field_country']['#object'];
After that, I could access it as a normal array.
Attributes of an object in php must be accessed by ->property_name. And, when using associated arrays, using ['key_name'].
Here, $user_object[0]['user_relationship_ui']['#type'] gives you the #type.
Don't confuse objects with associated arrays.
EDIT:
Further explaining,
and see line 18, 19:
[field_country] => Array (
[#theme] => field
see line 31, 32:
[#object] => stdClass Object (
[uid] => 1
The difference is if you want to access #theme, you have to do it by using ['#theme']. And if you want to access uid, you have to do ['#object']->uid.
Related
I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.
So it's really simple code and I am yanking my hair out with the problem.
//I first retrieve some JSON info (confirmed to work fine)
$file=file_get_contents('url');
//I then decode and print to verify (still working)
$somename=json_decode($file);
I print it out just to make sure it works (it does):
print_r($somename);
The print out reads as follows:
stdClass Object ( [id] => 456456456 [name] => somename [Stuff01] => 55 [Stuff02] => 25 [Stuff03] => 123132123132 ) )
Now I just want to get the value in the 'id' key so I use the appropriate object call:
$thisID=$somename->{'id'};
But I get the error:
Notice: Undefined property: stdClass::$id
I print_r every time so I know it's there. I can see it. What am I doing wrong?
I have had no problems doing this exact thing many times.
How do you access to individual properties is based on your data structure. You have kind of nested structure, object in object. Try like this:
$somename->somename->id;
//or
$yourObjectName->somename->id;
I hope this helps!
I have a quick question about something I imagine must be pretty easy - I've done a little research and found some links that seem promising, especially this, but it doesn't work for me for some reason.
Anyway, I made a stored procedure in MySQL and tested it with MySQL Workbench, and it works - it just adds num1 and num2 and returns the result. Now I'm trying to get it to work in PHP, but the result, instead of being an integer, is an array of one stdClass Object which contains that integer. That makes sense from the point of view of procedures that return a lot of data, but I'm having some trouble getting down to just the integer.
I run this:
CALL database.routine(2,7)
And I save the results into $var. When I run print_r($var), I get:
Array
(
[0] => stdClass Object
(
[num1+num2] => 9
)
)
So, to get past the Array part, I specifically asked for the first element in it, by running print_r($var[0]), which gets me:
stdClass Object
(
[num1+num2] => 9
)
And now I need to go one level deeper...I tried what the page I linked to above said and attempted to get to $var[0]->[num1+num2], as the field appears to be named, but that doesn't work. I've also tried a few combinations of single quotes and double quotes, but no luck. How do I get the number 9 out of this object?
Try...
$var[0]->{'num1+num2'}
Try this
$var[0]->{"num1+num2"}
or
$prop = 'num1+num2';
$var[0]->$prop;
Did you try this?
$var[0]->{'num1+num2'}
Try the following $var[0]->{'num1+num2'}
This is crazy but it might just work!
$var[0]->{'num1+num2'}
I googled, installed Devel, Drupal for Firebug, but I can't find it.
I found what I want, I know where it is; I just don't know how to get it.
I'll put this in code brackets, but Devel tells me the file name (which I want to stick into the .tpl.php file) is here:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
So, how do I get that FILENAME.jpg to be output using PHP?
<?php print $something->other; ?>
Whenever you need to read a value out of a variable, you need to know which expression you need to formulate to access that value.
For a simple variable value this is simple, you just take the variable name and access it as a variable by prefixing it with the $ sign:
var_dump($variable);
This is documented here.
However this does only work for simple datatypes like string or integer. There are as well compound datatypes, namely array and object. They can contain further datatypes, be it simple or compound. You can learn in the PHP manual how to access the values of an array and how you can access them from an object. I think you already know of that a bit, so just for having it linked here.
When you have learned about that, you can then combine this. E.g. if there is an array within an object and therein is a string you would like to get, you need to combine the $ sign and the variable name with the needed accessors, property names and array keys. Then you get your value. The data you have posted shows that you have an object that has some other objects and arrays and in the end you find the variable name.
Some combination example:
var_dump($variable->handler->view[0]->_field_data);
This is based on the data you've provided above. $variable is where you start, -> is used to access object members which need to be named then (like a name for a variable) : handler. As you've seen in your debug output that handler is an object, you need to use again the -> to access the view member of it.
Now view is different because it's an array. You access values of an array by using [] and putting the key in there. The key in my example is a number, 0. And as the value of that array entry is an object again, in the next step you need to use -> again.
You can continue this game until you reach the element that you're interested in. The debug output you already have helps you to write the expression that returns the value. Possibly it is:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
But I can not validate that here on my system in full.
However when finding things out, it's helpful to make use of var_dump as you could step by step extend the expression until you find the element. If you make an error you will immediately see. Sometimes it helps to place a die(); after the var_dump statement so not to end the response before it contains to much other data that will hide the information from you. The devel plugin offers additional debug routines to dump values prominent.
If this is your object:
field_image (Object) stdClass
handler (Object) views_handler_field_field
view (Object) view
result (Array, 2 elements)
0 (Object) stdClass
_field_data (Array, 1 element)
nid (Array, 2 elements)
entity (Object) stdClass
field_image (Array, 1 element)
und (Array, 1 element)
0 (Array, 11 elements)
filename (String, 23 characters ) FILENAME.jpg
I'd guess you can find it using:
field_image->handler->view->result[0]->_field_data['nid'][entity]->field_image['und'][0]['filename]
Could be a mistake in there, but the general Idea is: if you have an object, get the variable using ->, and if you have an array, use [key].
Let's say you have a node object in $node. You can print it's values very nice with:
dpm($node); // remember this function is declared in devel module
Then you can see the information from $node and expand the internal fields with a click. And with a double click on the field you can see it's php path.
You'll get this result:
Hope that helps!
PD: I guess this functionality isn't available on D6's dpm.
Try:
$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
If you have devel installed and try
krumo ($variable);
Just bear in mind as default only admin users have rights to use the krumo command, but this can be sorted out by looking at the DEVEL role permissions. (don't forget to remove these permissions once your done though)
<? print_r($something["other"]); ?>
(where other is this)
so result is 'this'
Let me summarize up
print_r($data); => Traditional view of printing array.
var_dump($data); => Not so much cleaned view , gives you everything but in very suffocated manner
print "<pre>"; print_r($data); => A cleaned view but will not get data types information.
dpm($data); => It gives you everything, but you need to have installed devel.
You should use field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL) , that will return renderable array . You can check api document https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_view_field/7.x
If you can't use devel module for some reason, another useful "debug" functions could be var_export() and a Drupal wrapper drupal_var_export(). These functions gives the output as PHP code.
I'm learning PHP and Drupal. I need to reference a variable contained in an array called $contexts.
So print_r($contexts) gives me this:
Array (
[context_view_1] => ctools_context Object (
[type] => view
[view] => view Object (
[db_table] => views_view
[result] => Array (
[0] => stdClass Object (
[nid] => 28
[node_data_field_display_field_display_value] => slideshow
)
)
eek confusing. I want to work with the node_data_field_display_field_display_value variable. I think my code needs to be like this, but I know this isn't right:
if ($contexts['context_view_1']['view']['result'][0]
['node_data_field_display_field_display_value'] == 'slideshow') then do whatever...
Thanks!
You suggested the following array reference to get to the variable you want:
$contexts['context_view_1']['view']['result'][0]['node_data_field_display_field_display_value']
The reason this doesn't work is because some of the structures in the chain are actually objects rather than arrays, so you need a different syntax for them to get at their properties.
So the first layer is correct, because $contexts is an array, so context_view_1 is an array element, so you'd get to it with $contexts['context_view_1'] as you did.
But the next level is an object, so to get to view, you need to reference it as an object property with -> syntax, like so: $contexts['context_view_1']->view
For each level down the tree, you need to determine whether it's an object or an array element, and use the correct syntax.
In this case, you'll end up with something that looks like this:
$context['context_view_1']->view->result[0]->node_data_field_display_field_display_value
That's a mess of a variable. The issue you're having is that you're using the bracketed notation, e.g. "['view']", for each "step" in the navigation through your variable. That would be fine if each child of the variable were an array, but not every one is.
You'll note, for example, that $contexts['context_view_1'] is actually an object, not an array (take note that it says "[context_view_1] => ctools_context Object"). Whereas you would use that bracketed notation to address the elements of an array, you use the arrow operator to address the properties of an object.
Thus, you would address the field you are trying to reach with the following expression:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
For properties listed as "Object", you need to use -> to get into it, and "Array", you need to use []. So:
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
$contexts['context_view_1']->view->result[0]->node_data_field_display_field_display_value
echo $context['context_view_1']->view->result[0]->node_data_field_display_field_display_value;
Do not mistake objects with arrays. A memeber of an array can be accesed by $array['member'], but fields of an object can be accessed as $object->fieldname.