DPM Once

Posted on: February 11th, 2014 by admin No Comments

dpm is a function provided by the devel module that allows you to inspect objects and arrays in Drupal’s message area.

Sometime I want to inspect something in a loop, but I only want to inspect the element once, not in every iteration of the loop. Especially it it is going to loop lots of times as sending that much data to the browser can make it crash (especially if it’s Firefox!)

To get round this problem I wrote a function called dpm_once:

1
2
3
4
5
6
7
function dpm_once($var) {
	$done = &drupal_static(__FUNCTION__);
	if (!isset($done)) {
		dpm($var);
		$done = true;
	}
}

The first time it is called it will log the variable using dpm, however all subsequent called won’t do anything at all.