Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 358 Vote(s) - 3.37 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Able to see a variable in print_r()'s output, but not sure how to access it in code

#1
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:

<pre>
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
</pre>

So, how do I get that FILENAME.jpg to be output using PHP?

<?php print $something->other; ?>
Reply

#2
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]`.
Reply

#3
Try:

$field_image->handler->view->result[0]->_field_data['nid']['entity']->field_image['und'][0]['filename']
Reply

#4
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][1].

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][2] and how [you can access them from an object][3]. 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.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#5
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)
Reply

#6
<? print_r($something["other"]); ?>

(where other is this)
so result is 'this'
Reply

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

![Devel Information of a Drupal node object][1]

You'll get this result:

![Devel showing the path after double clicking a field][2]

Hope that helps!

*PD: I guess this functionality isn't available on D6's dpm.*


[1]:

[2]:
Reply

#8
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.
Reply

#9
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

[To see links please register here]

Reply

#10
If you can't use devel module for some reason, another useful "debug" functions could be [var_export()][1] and a Drupal wrapper [drupal_var_export()][2]. These functions gives the output as PHP code.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through