8

I just spent a lot of time finding an element in a render array. I usually use dpm() to print the array, and then expand the options until I find what I'm looking for.

On larger arrays that's cumbersome. When I use print_r(), I can do a text search, but then it's hard to see the structure of the array and find the path to the element.

What other methods can you suggest?

5
  • This Question is worth to add my full reps !! – niksmac Apr 12 '12 at 6:14
  • There are some modules to pretty up all this caca. I am too lazy to go look but dev module integrates with krumo and other dump-enhancers, if I remember correctly. Then there's always a good old debugger, where you can actually inspect and drive down in objects even. – stefgosselin Apr 12 '12 at 7:53
  • @stefgosselin OP is already using the Devel module with dpm(), I think he's looking for a function to find a sub-element within a render array by a given name/key – Clive Apr 12 '12 at 8:21
  • @MotoTribe If I've got that right and you're looking for a function, what sort of input/output would you be hoping to give/get from it? Would it be something like $elements = find_elements_by_key($render_array, 'element_name'); to return an array of all of the elements in the render array with a matching key? – Clive Apr 12 '12 at 8:23
  • correct. In devel the "krumo display = disabled" shows a better formatted array where I can use a text search. Ideally there should be a search function in the krumo output and a "copy" feature where I can copy the full array path ['...']['...'] without having to type it. Yeah, your function would be awesome too. That's not a Drupal or PHP function, is it? – uwe Apr 12 '12 at 13:30
9

There is extension module for Devel: Search Krumo. It uses Javascript to find text in Krumo/dpm messages and open array in those places.
Looks like this:
enter image description here

Update

After 22 Apr 2012 you should use sdpm() function to see search box.

5
  • no way, that's exactly what I was looking for!!! Thanks!!! – uwe Apr 18 '12 at 15:58
  • @MotoTribe, so it can be accepted, I think :) – kalabro May 20 '12 at 14:37
  • sorry, missed that one. This is an awesome tool, use it all the time now!!! – uwe May 20 '12 at 17:01
  • where i put this extension module? either inside devel directory [\www\drupal\sites\all\modules\devel]or inside contributed modules directory[\www\drupal\sites\all\modules] – GiLL Aug 8 '12 at 9:49
  • Could you please mention that how to use sdpm() function? where to paste? Very Thanks. – CodeNext Sep 4 '16 at 5:37
5

One of the big problems with render arrays is that there are often a bunch of read-only copies of the information that you're looking for. If you want to actually change the render output, you have to find the right one. I usually use dpm(), and then view page source and search through the output there, flipping back and forth between the two displays until I find what I need.

If you want to dump the contents of a render array for a given path to the terminal (e.g. to pass it through grep), you could do something like this:

drush ev '$router_item=menu_get_item("node/7"); require_once(DRUPAL_ROOT .
"/" . $router_item["include_file"]);
$render_array=call_user_func_array($router_item["page_callback"],
$router_item["page_arguments"]); print_r($render_array);'

You could also just 'view page source' in your browser and use the clipboard, of course, but the above might be the start of a Drush command to view render arrays.

Update: I found a really good article on this subject: The Scary Render Array -- probably the best treatment of render arrays I have seen so far. I should have also mentioned the devel_themer module upfront; it does a good job at "zooming in" on the section of the render array you are interested in.

1
  • Can't thank you enough for linking to The Scary Render Array - great article! – Andy Apr 17 '12 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.