PHP Magento Tips

Add Custom Module to Global Search In Magento Admin Panel

Rate this posting:
{[['']]}

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 Training_Animal. We will be adding functionality to search by animal name within Magento’s admin area global search box.

Step 1:

In app/etc/modules/Training_Animal.xml add below code:

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

Step 2:

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

In app/code/local/Training/Animal/etc/config.xml add below code:

<config>

    <modules>

        <Training_Animal>

            <version>1.0.0</version>

        </Training_Animal>

    </modules>

    <global>

        <models>

            <training_animal>

                <class>Training_Animal_Model</class>

            </training_animal>

        </models>

    </global>

...................

    <adminhtml>

        <global_search>

            <animal>

                <class>training_animal/adminhtml_search_animal</class>

                <acl>training_animal</acl>

            </animal>

        </global_search>

    </adminhtml>

...................

</config>

In our config.xml file you’ll notice we are defining "animal", 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.

Step 3:

In app/code/local/Training/Animal/Model/Adminhtml/Search/Animal.php add below code:

<?php

// app/code/local/Training/Animal/Model/Adminhtml/Search/Animal.php

class Training_Animal_Model_Adminhtml_Search_Animal extends Varien_Object

{

   

    public function load()

    {

        $arr = array();

        if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {

            $this->setResults($arr);

            return $this;

        }

        $collection = Mage::getResourceModel('training_animal/animal_collection')

            ->addFieldToFilter('name', array('like' => $this->getQuery().'%'))

            ->setCurPage($this->getStart())

            ->setPageSize($this->getLimit())

            ->load();

        foreach ($collection->getItems() as $animal) {

            $arr[] = array(

                'id'          => 'animal/1/'.$animal->getId(),

                'type'        => Mage::helper('training_animal')->__('Animal'),

                'name'        => $animal->getName(),

                'description' => $animal->getName(),

                'url' => Mage::helper('adminhtml')->getUrl(

                    '*/animal/edit',

                    array('id'=>$animal->getId())

                ),

            );

        }

        $this->setResults($arr);

        return $this;

    }

}

Here we can see we are loading our model collection class and then apply a filter to the collection, to search by database field names “name”, using the like condition which will be 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.
  • Type: This is the type of search result - shown in red in search results block, e.g. [Animal]
  • 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: Here pass correct url for your controller for edit action. So when you select search result it will go to your Edit page. [ Here my controller path is app/code/local/Training/Animal/controllers/Adminhtml/AnimalController.php]

With this example you can add as many global search nodes as you wish.

Reference: http://blog.mattstephens.co.uk/post/27326981315/adding-custom-module-to-magentos-admin-global-search

Tag : Magento
0 Comments On "Add Custom Module to Global Search In Magento Admin Panel"

Back To Top