Bosworth99
Bosworth99

Reputation: 4234

Magento - custom module structure

Heyo, been working on a magento theme for a little while now (... and my understanding of the system is slowly but steadily growing). I need to start working on some custom modules soon, and so I'm starting with this little project to get my bearings:

I found a little bit of code to manage a specific task that I have been copying and pasting into template files:

<?php         
    $ids    = $_product->getCategoryIds(); 
    $cat    = Mage::getModel('catalog/category')->load($ids[0]);     
    $isFF   = false;
    foreach ($ids as $key=>$val) {
        $cat = Mage::getModel('catalog/category')->load($val);
        $name = $cat->getName(); 
        if ( $name === 'Fast Fulfillment' ) {
            $isFF = true;
        }
    }
    if ($isFF) { echo '<span class="ff-logo"></span>'; } 
?>

Very simple. I'm just checking to see if a product is in a specific category, and producing an element if it is. (I know there are about 5 solid ways of doing this... this is the one I went with).

I need to test this every time a product block is displayed, and have, up till now, been replicating this code to make it work. It does work, but is backasswards (I shouldn't be putting logic into the view layer).

Ok - so, lets make a simple module to share the functionality:

local/WACI/ProductFlag/etc/config.xml

<config>
    <modules>
        <WACI_ProductFlag>
            <version>0.1.0</version>
        </WACI_ProductFlag>
    </modules>
    <global>
        <blocks>
            <WACI_ProductFlag>
                <class>WACI_ProductFlag_Block_View</class>
            </WACI_ProductFlag>
        </blocks>
    </global>
</config>

etc/modules/WACI_All.xml

<config>
    <modules>
        <WACI_CustomPageLayouts>
            <codePool>local</codePool>
            <active>true</active>
        </WACI_CustomPageLayouts>
    </modules>
    <modules>
        <WACI_ProductFlag>
            <codePool>local</codePool>
            <active>true</active>
        </WACI_ProductFlag>
    </modules>
</config>

Now, for the Class ... I'm not really sure if a Block or a Helper is appropriate. I went with Block, but - idk... I'm probably wrong (tutorials for this stuff vary wildly).

local/WACI/ProductFlag/Block/View.php

<?php

    /**
     * WACI
     * 
     * @codepool   Local
     * @category   View
     * @package    WACI
     * @module      ProductFlag  
     */

    class WACI_ProductFlag_Block_View extends Mage_Core_Block_Template
    {
        private $_focus;
        private $_isFF = false;

        public function getIsFF( $product ){
            $this->_focus = "FF";
            $isFF   = false;
            $ids    = $product->getCategoryIds(); 
            $cat    = Mage::getModel('catalog/category')->load($ids[0]);   

            foreach ($ids as $key=>$val) {
                $cat = Mage::getModel('catalog/category')->load($val);
                $name = $cat->getName(); 

                if ( $name === 'Fast Fulfillment' ) {
                    $isFF = true;
                }
            }
        }

        protected function _toHtml(){
            $html = parent::_toHtml();

            if ($this->_focus === "FF") {
                if ($this->_isFF){
                    $html .= '<span class="ff-logo"></span>';
                }
            }

            return $html;
        }
    }
?>

Hopefully its clear that I only want to output a string based on the input of any given product. Should I be overriding the _toHtml() to deliver the string? Again. Probably not...

in my local.xml I reference the block:

<catalog_product_view>
<reference name="content">
            <reference name="product.info">
<block type="WACI/ProductFlag" name="product.flag" as="productFlag" output="toHtml" />...

... I'm not clear if this instantiates this class? I don't think so. I'm not really sure how to address it in my product/view/media.phtml anyway, given that I need to call a method with a parameter.

I don't think I need a template file, given that I'm just outputting a string, but I don't think I've seen block modules without an associated template. Maybe a helper class is appropriate then?

Whatever the case, it ain't workin.
I either get a fatal error saying my media class has no getIsFF() method (not surprising), or just nothing shows up. My config files are correct, but that's about it, I think.

Jebus. I'm all turned around.

Can someone with Mage skillz clarify this simple issue and point me in the right direction?

Cheers!

Upvotes: 2

Views: 829

Answers (1)

Bosworth99
Bosworth99

Reputation: 4234

No advice? Well = I sussed out this slightly modified and working solution:

local/WACI/ProductFlag/etc/config.xml

<config>
    <modules>
        <WACI_ProductFlag>
            <version>0.1.0</version>
        </WACI_ProductFlag>
    </modules>
    <global>
        <blocks>
            <productflag>
                <class>WACI_ProductFlag_Block</class>
            </productflag>
        </blocks>
        <helpers>
            <productflag>
                <class>WACI_ProductFlag_Helper</class>
            </productflag>
        </helpers>
    </global>
</config>

local/WACI/ProductFlag/Helper/Flag.php

class WACI_ProductFlag_Helper_Flag extends Mage_Core_Helper_Abstract
    {
        private $_isFF = false;

        public function getIsFF( $product ){

            $html   = '';
            $ids        = $product->getCategoryIds(); 
            $cat        = Mage::getModel('catalog/category')->load($ids[0]);   

            foreach ($ids as $key=>$val) {
                $cat = Mage::getModel('catalog/category')->load($val);
                $name = $cat->getName(); 

                if ( $name === 'Fast Fulfillment' ) {
                    $this->_isFF = true;
                }
            }

            if($this->_isFF) { 
                $html = '<span class="ff-logo"></span>';
            }

            return $html;
        }

    }

and call it up in any template file via the simple:

<?php echo $this->helper('productflag/flag')->getIsFF($_product); ?>

I'm still not sure if this is exactly appropriate to the magento way of doing things - ie the model calls, I think, should be relegated to their own class and dropped in a Model folder.

Whatever the case - and for anyone else trying to figure this stuff out - as I monkeyed around with it I slowly realized the intent of the config.xml file is to add available factory classes to the blocks/helpers/model pools - and that the path is to the containing directory. The helper call in the template file, then, identifies the "short name" of the directory and then the actual class name.

ie - You could have multiple classes of helpers:

<?php echo $this->helper('productflag/class_one')->someMethod($_product); ?>
<?php echo $this->helper('productflag/class_two')->someOtherMethod($_product); ?>
<?php echo $this->helper('productflag/class_three')->yetAnotherMethod($_product); ?>

So... One step closer.

Upvotes: 3

Related Questions