Search code examples
phpmagentowidgetshortcodemagento-1.4

Need to extract parameters from widget shortcodes used in Magento


In a project I have bought an extension which is using widgets and their short-codes. I have to extract the parameters from the widget short-code so that I can use them in my module for mapping some block IDs. How can I extract the parameters from the widget short-code string into an array in the following?

{{block type="cms/block" block_id="footer_links" template="ABC/roam/dp.phtml"}}

Solution

  • This took me a while for searching how the CMS content area gets the widget short-codes working. Here is how I decoded the short-code. I have made a helper for this.

    public function widgetToArray($widget)
    {
        $tokenizer = new Varien_Filter_Template_Tokenizer_Parameter();
        $cons = str_replace(array("{{","}}"),array("",""),$widget);
        $tokenizer->setString($cons);
        $params = $tokenizer->tokenize();
        foreach ($params as $key => $value) {
            if (substr($value, 0, 1) === '$') {
                $params[$key] = $this->_getVariable(substr($value, 1), null);
            }
        }
        return $params;        
    }
    

    Hope some day this might help some one.