0

I would like to get all the columns infos of the product grid columns from my own helper.

I tried (among others) with the following code :

Mage::getDesign()->setArea(Mage_Core_Model_App_Area::AREA_ADMINHTML);
$gridBlock = Mage::app()->getLayout()->createBlock('adminhtml/catalog_product_grid');

If I log then $gridBlock->debug(), I obtain

Array
(
    [row_click_callback] => openGridRow
    [id] => productGrid
    [use_ajax] => 1
    [type] => adminhtml/catalog_product_grid
)

but the return of $gridBlock->getColumns() is an empty array.

In other words, I'm trying to simulate the creation of the product grid in order to get its columns informations.

Any ideas on how to do it ?

1

The columns property is not populated until you call the toHtml method of the block.
The method _prepareColumns, the one that builds the columns is called in the _prepareGrid method that is called in _beforeToHtml that is called in toHtml.

So you can try

Mage::getDesign()->setArea(Mage_Core_Model_App_Area::AREA_ADMINHTML);
$gridBlock = Mage::app()->getLayout()->createBlock('adminhtml/catalog_product_grid');
$gridBlock->toHtml();
$columns = $grid->getColumns();

but there is a chance you will get errors if you are out of context.
Anyway... It's cheap to try it.

  • Thanks @Marius, you pushed me on the right way. I fact I've already seen that the only public method I could call was the toHtml method. But, as you say, I gave up because there was error (some weird PHP Fatal error: Maximum function nesting level of '100' reached, aborting! on server side). Following your comment "you will get errors if you are out of context", I tried with some $appEmulation = Mage::getSingleton('core/app_emulation'); workaround; I will post my answer but yours was very usefull. – Julien Loizelet Feb 27 '18 at 14:00
1

Well,

with the help of Marius answer, I succeed with the following code :

$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(Mage_Core_Model_App::ADMIN_STORE_ID, Mage_Core_Model_App_Area::AREA_ADMINHTML);
$gridBlock = Mage::app()->getLayout()->createBlock('adminhtml/catalog_product_grid')->setTemplate('');
$gridBlock->toHtml();
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

Then $gridBlock->getColumns() returns what I was expecting.

Note that the emulating part may be unnecessary if you run this code from the admin part. So, it should work with this :

$gridBlock = Mage::app()->getLayout()->createBlock('adminhtml/catalog_product_grid')->setTemplate('');
$gridBlock->toHtml();

Edit 1 : I updated my answer as I just saw that there was an error in the system.log : CRIT (2): Not valid template file:frontend/base/default/template/widget/grid.phtml. In fact, I should pass a second parameter (Mage_Core_Model_App_Area::AREA_ADMINHTML) in the startEnvironmentEmulation method to ensure that design will be build from the admihtml area. But when I do that, I get again the fatal error : PHP Fatal error: Maximum function nesting level of '100' reached, aborting. So, I made it work without error by setting a "fake" template : $gridBlock->setTemplate('page/head.phtml'). I hope I will find a cleaner way to do that and I will edit my answer in this case.

Edit 2 : Well, it seems the error Maximum function nesting level of '100' reached I have when I don't change the template is due to some infinite loop that begins in the app/design/adminhtml/default/default/template/widget/grid.phtml (especially with the massaction part). If I comment this part, it works with just a

$gridBlock = Mage::app()->getLayout()->getBlockSingleton('adminhtml/catalog_product_grid');
$gridBlock->toHtml();

Moreover, as I launch this code from the admin area, I can even delete all the $appEmulation part. I don't remove it from my answer as it can be usefull for someone who wants to launch it from the frontend.

Finally, to be "cleaner", instead of calling the page/head.html, I will create my own "fake" template and call it. I didn't find any observer that allow me to remove the massaction block properly.

Edit 3 : With a setTemplate(''), it works too without any error. I update my answer with this workaround.

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.