3

I'm creating a template for a widget and would like to echo the widget title in my PHP code. What is the proper way of doing that?

The following does not work:

<?php echo $this->getTitle() ?>

Note: this is part of a custom PHTML file in my theme.

1
  • I tried adding the following before this line, but it didn't help: <?php $_widget = Mage::getModel('widget/widget_instance')->load($widgetId); ?> Commented Dec 23, 2015 at 6:12

3 Answers 3

1
<?php $widgetId =1;
$widget = Mage::getModel('widget/widget_instance')->load($widgetId);
echo $widget->getTitle(); ?>

Try this it's working...

0

Try this

<?php $widgetId =1;?>
<?php $widget = Mage::getModel('widget/widget_instance')->load($widgetId)); ?>
<?php echo $widget->getTitle()); ?>
9
  • This did not work for me. Commented Dec 23, 2015 at 5:49
  • Update ans pls check
    – Abdul
    Commented Dec 23, 2015 at 6:17
  • That just dumped an array with no values Commented Dec 23, 2015 at 6:18
  • pls add your widget Id in var $widgetId for eg. <?php $widgetId =1;?>
    – Abdul
    Commented Dec 23, 2015 at 6:20
  • OK, that works by specifying the exact widget ID. However, I need to load the current widget ID dynamically. If I can do that, this solution will work. Commented Dec 23, 2015 at 6:23
0

Hope this helps Michael, been trying to do the very same thing today. Eventually came up with a (convoluted) way of doing it... i'm not saying its the best, but this is how i got it done:

MySQL Method

SELECT wi.*
FROM widget_instance wi
LEFT JOIN widget_instance_page wip ON wip.instance_id = wi.instance_id
LEFT JOIN widget_instance_page_layout wipl ON wipl.page_id = wip.page_id
LEFT JOIN core_layout_update clu ON clu.layout_update_id = wipl.layout_update_id
WHERE clu.xml LIKE "%<name>unique_id</name><value>UNIQUE_ID</value>%"

PHP Method

 $unique_id = $this->getUniqueId();

$collection = Mage::getModel('widget/widget_instance')->getCollection();
$collection->getSelect()
  ->join(array("wip" => "widget_instance_page"), "main_table.instance_id = wip.instance_id")
  ->join(array("wipl" => "widget_instance_page_layout"), "wip.page_id = wipl.page_id")
  ->join(array("clu" => "core_layout_update"),"wipl.layout_update_id = clu.layout_update_id")
  ->where("clu.xml LIKE \"%<name>unique_id</name><value>".$unique_id."</value>%\" ");

$widget_id = $collection->getFirstItem()->getId();
$widget_title = Mage::getModel('widget/widget_instance')->load($widget_id)->getTitle();
1
  • This didn't work for me doing a copy/paste of the PHP code at the top of my widget PHTML file. I just get an empty title. Specifying an ID directly works, so it seems there is some issue dynamically obtaining the ID of the currently loaded widget. Also, I'm pretty sure it is bad practice to run SQL scripts from a template file, although that seems to be the only option at this point (if it works). Commented Jun 2, 2016 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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