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.
Related
While doing some code refactoring I momentarily ended up in a situation where I was basically doing the (somewhat abstracted-out) equivalent of
$data = (object)json_decode('"test"');
Of course I understand json_decode() generates objects on its own unless assoc is false. (Incidentally I got into this situation because I was in the middle of moving some format processing code around, and I hadn't yet realized one of my (object) casts was now redundant.)
But... when this happened, PHP decided that $data contained:
stdClass Object
(
[scalar] => test
)
Wat.
scalar?!
Last I learned, "test" is a string, so it seems more than one pile of things has fallen over internally here. Or is this unintuitive yet intended design?!
I have of course removed the (object) and things work exactly how I intended now. So there's no bug here per se. I just wanted to understand what just happened.
Here you go, in case you want to join in the headscratching:
php -r 'print_r((object)json_decode("\"test\""));'
I'm using 7.0.25.
This is exactly what the manual specifies will happen when casting a scalar type (i.e. int, string, float, boolean) to an object.
If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.
For any other value, a member variable named scalar will contain the value.
$obj = (object) 'ciao';
echo $obj->scalar; // outputs 'ciao'
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've seen this problem a few times, and would like a definitive answer.
Given this structure in xml:
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
I am not able to access the attribute 'order'. var_dump (unsurprisingly) does not show any attributes for ByteListSet. Indeed, foreach iteration does not produce a #attributes item.
However, the following structure:
<ByteListSet>
<Byte Order="0"><Value>3729</Value></Byte>
</ByteListSet>
Results properly in ByteListSet having a child Byte that is a SimpleXmlObject which has #attributes.
I would assume that SimpleXML is indeed picking up the #attributes from the first case, but where is it keeping them? The trouble is that in the former structure, ByteListSet produces this on var_dump of ->children():
object(SimpleXMLElement)[25]
public 'Byte' => string '14' (length=2)
if I get_object_vars() on it and var_dump each, I simply get:
string '14' (length=2)
Indicating that Byte is not being returned to me as an xml object, but just as a string; as a property of the ByteList object above it.
Order="0" is there somewhere, but I don't have access to it. How do I get to it? NOTE: ->attributes() returns, as you would expect, a blank array.
(We do not control this schema, so it can't be restructured.)
You wrote:
Given this structure in xml:
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
I am not able to access the attribute 'order'.
Sure, because the attribute order does not exist. XML is case-sensitive, the attribute is named Order with uppercase O at the beginning.
Using the right attribute name allows you to access the value as outline in Example #5 in the basic examples of the SimpleXML extension you can find in the manual:
$ByteListSet = simplexml_load_string(<<<XML
<ByteListSet>
<Byte Order="0">14</Byte>
</ByteListSet>
XML
);
echo $ByteListSet->Byte['Order']; # 0
Please keep in mind that what you see in var_dump or print_r is not always what you get. This is espeically true for Simplexml (compare: How to get values of xml elements?; SimpleXML and print_r() - why is this empty?), however, also for other objects in PHP that make use of ArrayAccess, __toString() or IteratorIterator / IteratorAggregate.
Please let me know if that answers your question. You were not very specific which existing solutions you have not understood so far, so I answered your question, but I'm not 100% if I hit the nail.
I'm working with an object fed back from a class. For some reason, the class spits out an object with numbered properties (0, 1, 2, etc.). I need to check if the object is empty. The standard trick, empty(get_object_vars($obj)), won't work, because get_object_vars returns an empty array even when the object has (numbered) properties.
For reference, the object I'm working with is the one returned by the legislatorsZipCode method of the PHP interface for Sunlight's API. You can see a print_r of a sample response here.
Judging by the code, the author made the mistake of casting a numerically indexed array to an object. This makes getting object properties by name impossible, though you should still be able to foreach over it. You can also simply cast it back to an array: $results = (array) $obj;. Then, count the array.
This appears to work:
if (current($obj) === false)
echo "is empty\n";
It probably is doing an implicit cast to an array.
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.