Drupal 7 Search Krumo Module
Share with Others
The Drupal 7 Search Krumo Module extends the Devel module and makes it easier to debug PHP variables while developing Drupal modules or building Drupal themes. It adds a simple search box that allows you to quickly search through the Krumo output that is generated by the dpm function.
If you want to try it out quickly, simply download and install the module and run the following code on the variable you want to debug:
You can also use the sdpm function to make grabbing the PHP variable path easier.
Hello everyone and welcome to another Daily Dose of Drupal. Today we’re on Episode Number 171 and we’re going to be going over a few module development tips to help you easily debug variables and when you’re building a Drupal modules and when you’re working with PHP.
As always I’m Shane Thomas, you can follow me on Twitter at smthomas3, also make sure to check out codekarate.com, take a look at the EBook and look at all the other videos and post on the website. If you have been developing Drupal Modules for a while or just getting started, you may have heard of the Devel Module.
This is a very helpful module to easily help you develop Drupal Modules and debug code as you are working through building your Drupal Modules. But today we’re going to look at the Search Krumo Module, adds on to the Devel Module and makes it just a little bit better.
The first thing we’re going to do is take a look at our Drupal website. We have a simple Drupal 7 website and for this example we’re just going to assume we want to take a look at this little search form here. We just want to look at the form array that builds this search form and we want to use the Search Krumo Module and see how that can help us do that.
The first thing is to look at the modules we have installed. I have the Devel Module turned on right now. I do not have the Search Krumo Module on yet, we’ll be turning that on shortly. I also have a simple example module. So let’s go ahead and take at what is in that example module. Just a really simple info file and the module file has a Hook Form Alter function and I have some code that checks to see if it’s the Search Block form because we only want to look at that specific form.
So typically, the way I’ve done it in the past and I still do it sometimes to this day is I use a print statement, something like this. So I may print out the form variable, if I click Save, if I come back to our Drupal site here, if I refresh I’m going to see the output. As you can see I get output saying it’s an array, there’s a search block form element, it’s a text field you can go through and I can see all of the information. This is kind of nice sometimes because you can see it laid out in the structure.
However it very quickly becomes a little unmaintainable or unmanageable when it gets to be very long. There’s a lot of forms or variable you will debug in Drupal that are extremely long and just not practical to use a print statement like that. With Devel you can use a DPM function which is a simple print function that prints out a nice easy to click through option.
So if we use the DPM function instead of just the simple print function you’ll notice in the status area of your Drupal site there is the variable printed out you can click through. So I can see there’s a search block form, I can even see the default value here we can click through and see all of the different…basically all the different options and settings for that specific array that we’re going to take a look at. So that’s another option that comes with you with the Devel Module. Searching through this can still become a problem when it gets to be very long and that is where the Search Krumo comes in.
So let’s go ahead and turn that module on and click Save and keep in mind with my code I still just have this DPM function here and I’m printing out the form variable. Now if I come back here and I refresh you can see there’s a search box here. Everything else looks the same, you can see I have this little Get Path option. Let’s go ahead and search for default value. If I search you can see it highlights every word, it finds the default value. You don’t have to search just the index of the array. You can actually search for values within the array so I can search for instance Terms here and then you have the terms 1 highlighted.
So as you can see that works out really well and makes it very easy. You can notice if I hover over a Get Path it’s going to allow me to copy this variable. If I wanted to change the default value I just click the Get Path, I can copy that, paste it in my code. Let’s say I want the default value to Search. I do need to change this Var to Form because that’s the name of my variable. I will come at this out for a second. Now if we come back here, the default value is now Search.
There’s also another function that is added when you use the Search Krumo Module and it’s the SDPM function. It’s almost the exact same thing, there’s only a very small difference so you can use that. You can still search for the default value, it will open it up and show you. Now if you click Get Path you’ll notice it replaces the variable name with the correct variable name.
So now I copy this over, I can paste that in and I can say I want this to be search with an exclamation point this time instead of just Search, comment that out and refresh. I forgot a semicolon here so I’ll add that semicolon and refresh and now you have Search with an exclamation point as the default value for that form or that text field.
So that’s really all there is to it with the Search Krumo Module are really simple but extremely handy module if you’re developing themes or modules with Drupal. Anytime you’re working with PHP variables on a Drupal site, the Search Krumo Module is incredibly handy. So go ahead and try it out, let me know what you think. If you have ideas for future videos let me know, make sure to check out codekarate.com and we will see you next time.