0

When I do $this->getLayer() in a block template, I get a lot of output but I don't understand it.

I'm trying to make my own module for displaying products based on certain queries and want to understand this getLayer() reference.

The code where I saw getLayer() was:

 protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            $origCategory = null;
            if ($this->getCategoryId()) {
             $category2B = $this->getNameInLayout
                //$category = Mage::getModel('catalog/category')->load($this->getCategoryId()); //important
                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }
            $this->_productCollection = $layer->getProductCollection();

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }

        return $this->_productCollection;
    }

I pretty much understand everything here except the layer. Is this referring to a model? Please help me understand exactly what this code is doing.

Whoops, turns out getLayer was defined later in the PHP file, but I still don't understand its function...

 public function getLayer()
    {
        $layer = Mage::registry('current_layer');
        if ($layer) {
            return $layer;
        }
        return Mage::getSingleton('catalog/layer');
    }
1

It will return the object stored in the Magento registry at key current_layer. The registry basically allows objects to access global variables. This value is saved into the registry in a few places, most-notably: \Mage_CatalogSearch_Block_Layer::_construct

/**
 * Internal constructor
 */
protected function _construct()
{
    parent::_construct();
    Mage::register('current_layer', $this->getLayer(), true);
}

Which calls

/**
 * Get layer object
 *
 * @return Mage_Catalog_Model_Layer
 */
public function getLayer()
{
    return Mage::getSingleton('catalogsearch/layer');
}

So, this is a singleton object of Mage_CatalogSearch_Model_Layer, or, if you're using Enterprise, Enterprise_Search_Model_Search_Layer.

This object is concerned with managing the layered (faceted) navigation in Magento. This is the left-side navigation which shows attributes and their values to filter the results by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.