This is either a feature request, or me sharing an awesome bit of code that I wish were in devel.

I just spent an excessive amount of time working on #2870463: Better way to theme paragraphs edit page, trying to track down a certain string in a giant hook_preprocess_HOOK $variables tree with kint. Paragraphs are very complicated and it's all to easy to go down blind alleys while hunting for the string you're looking for. kint also has a habit of hanging the browser on the system if you give it anything overly complicated.

I ended up writing a function to help me traverse a large $variable tree and find the string I was looking for: (Yes everyone, please *do* steal this. It's an awesome function.)

function hunter_seeker(&$variables, $search, $replace = TRUE, $prefix = "\$variables", $depth = 0, &$result = "") {
  if ($depth>10)
    return;
  if (is_array($variables)) {
    foreach ($variables as $key => &$value) {
      hunter_seeker($value,$search,$replace,$prefix . "['" . $key ."']", $depth+1, $result);
    }
  } else if (is_object($variables)) {
    $object_vars = get_object_vars($variables);
    foreach ($object_vars as $key) {
      if (isset($variables->$key)) {
        $value = &$variables->$key;
        hunter_seeker($value,$search,$replace,$prefix . "->" . $key, $depth+1,$result);
      }
    }
  } else if ($variables === $search) {
    $result .= "$prefix = \"$search\";\n";
    if ($replace) {
      if (is_string($variables)) {
        $variables = $prefix . " (" . $variables . ")";
      } 
    }
  }
  
  if (($depth == 0) && ($result != "")) {
    dpm("Instances of string \"$search\" in \$variables:\n\n$result");
  }
  
}

This function not only displays the locations of a string in a given $variable:

Instances of string "Retail Carton Information" in $variables:

$variables['element']['#title'] = "Retail Carton Information";
$variables['element']['0']['top']['paragraph_type_title']['info']['#markup'] = "Retail Carton Information";
$variables['table']['#header']['0']['data']['title']['#markup'] = "Retail Carton Information";
$variables['table']['#rows']['0']['data']['1']['data']['top']['paragraph_type_title']['info']['#markup'] = "Retail Carton Information";

But it will also take every instance of the string within the HTML and replace it with the reference to access it. (i.e. $variables['table']['#rows']['0']['data']['1']['data']['top']['paragraph_type_title']['info']['#markup'] = "$variables['table']['#rows']['0']['data']['1']['data']['top']['paragraph_type_title']['info']['#markup']", so the variable reference shows up in the HTML instead of the previous "Retail Carton Information")

I'm not saying this function is polished yet (It may not even be working for objects), but has devel ever considered including a function like this one? (Maybe it already has one?) It was incredibly useful for helping me with my recent problem.

Comments

TrevorBradley created an issue.

Bahson’s picture

I believe it's an awesome feature..was searching for something like this... but it doesn't work for me.. on objects and on traditional drupal arrays..Maybe a documentation on usage would help alot. Thanks