Magento Themes and Magento Extensions

Adding your custom module to Magento’s Admin Global Search

Share

When creating a custom module in Magento, have you ever wanted to add custom search functionality to the Magento admin area, allowing users to search your module? Today I will attempt to show you in the most simple of terms how you can achieve this and add to Magento’s global search.

For this example I will be using a custom module called Matt_Car, a fictitious module that may contain information on a car and output on the store front. We will be adding functionality to search by car name or brand within Magento’s admin area global search box.

I will assume you’ve already created a custom module, as this will be the module you are searching, but your module declaration should look something like…

app/etc/modules/Matt_Car.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Matt_Car>
            <active>true</active>
            <codePool>local</codePool>
        </Matt_Car>
    </modules>
</config>

Next in our module config.xml file we need to add a global search XML node, to tell Magento of our ‘new’ search logic!

You’ll also notice I’ve included a model declaration, this is only required if you haven’t already told Magento your custom module has model classes, and the appropiate way of declaring them (in this case ‘matt_car’).

app/code/local/Matt/Car/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Matt_Car>
            <version>0.1.0</version>
        </Matt_Car>
    </modules>
	<global>
        <models>
			<matt_car>
				<class>Matt_Car_Model</class>
			</matt_car>
		</models>
		...
	</global>
	...
    <adminhtml>
		...
        <global_search>
            <matt_car_test>
                <class>matt_car/search_test</class>
                <acl>matt_car</acl>
            </matt_car_test>
        </global_search>
		...
    </adminhtml>
	...
</config>

In our config.xml file you’ll notice we are defining “matt_car_test”, this is a unique name for our search, and can be anything you like. The “class” declaration tells Magento where to handle our search request (in this case Matt_Car_Model_Search_Test).

The final “acl” declaration stops users who don’t have admin acces control level (ACL) permissions to access the Matt Car module, the ability to search our module either! The value should be the XML node name defined in your ACL declaration. Again ACL’s are something you should already have setup in your module, so I won’t cover that here.

Now, lets create our search model class…

/app/code/local/Matt/Car/Model/Search/Test.php:

<?php
class Matt_Car_Model_Search_Test extends Varien_Object
{
    /**
     * Load search results
     *
     * @return Matt_Car_Model_Search_Test
     */
    public function load() {
        $arr = array();
		$searchText = $this->getQuery();
        $collection = Mage::getModel('matt_car/car')->getCollection()
            ->addFieldToFilter(
                array('name', 'brand'),
                array(
                    array('like'=>'%'.$searchText.'%'),
                    array('like'=>'%'.$searchText.'%')
                    )
                )
            ->load();

        foreach ($collection as $model) {
            $description = strip_tags($model->getDescription());
            $arr[] = array(
                'id'            => 'matt_car/test/'.$model->getId(),
                'type'          => Mage::helper('adminhtml')->__('Car'),
                'name'          => $model->getName(),
                'description'   => Mage::helper('core/string')->substr($description, 0, 30),
                'url' => Mage::helper('adminhtml')->getUrl('*/search_test/edit', array('id'=>$model->getId())),
            );
        }

        $this->setResults($arr);
        return $this;
    }
}

Here we can see we are loading our “car” model collection class (something your module should already have, a collection that is). We then apply a filter to the collection, to search by database field names “name” and “brand”, using the like condition, passing in our search query string.

For the collection results returned, we then loop through each and assign various values to our output array:

  • ID: A unique ID for the search link (can be anything you wish)
  • Type: This is the type of search result – shown in red in search results block, e.g. [Product]
  • Name: The text shown in bold in search results block
  • Description: This is the text shown below the bold heading in search element, you’ll note it is defined in a variable first to strip any nasties from WYSIWYG editors etc.
  • URL: It’s worth noting the URL should be the URL to your normal Magento admin page, in my example the full url would be index.php/admin/search_test/edit/id/19/key/2dfce21795ceba425f020b7a4a671181/. Simply copy the first section after the “/admin/” section in the modules URL. My example would map to app/code/local/Matt/Search/controllers/Adminhtml/Search/TestController.php but this is going off topic.

The results are then set against our object.

Simples! You should now be able to see your custom search results! You can add as many global search nodes as you wish, and of course make use of the standard Magento Collection methods on your search models.

How it works

If you’re interested in how this then gets used (feel free to ignore); In app/code/core/Mage/Adminhtml/controllers/IndexController.php in the globalSearchAction() Magento loops through each of the global search nodes from the XML config and returns the results (calling getResults() incidentally), merging them into a single array.

If you experience any errors, the chances are there is an error in your script… turn on debugging, and check Chrome/Firefox for your Ajax request responses!

Leave a Reply

You must be logged in to post a comment.