How to get a list of cms pages in Magento? - magento

What i'm trying todo
I have created an admin form where the user needs to select a CMS page from a drop down.
What i have tried
$form->addField('cms_page_id', 'select', array(
'label' => Mage::helper('custom/data')->__('CMS Page'),
'class' => 'required-entry',
'required' => true,
'name' => 'cms_page_id',
'values' => Mage::getSingleton('cms/page')->toOptionArray(),
'value' => $this->getCmsPageId()
));
The idea is the code gets the an option array from the CMS model. However "toOptionArray" is an invalid function for the 'cms/page' model.
My Question
How can I get an option array of CMS pages for use in an admin form in Magento?

With your code you are loading a new cms page model. To get a collection use following code and toOptionArray() will at least return something:
Mage::getModel('cms/page')->getCollection()->toOptionArray()

CMS Pages array with Links
$cms_arr = Mage::getModel('cms/page')->getCollection()->toOptionArray();
$cms_pages[""] = "-Select CMS Page-";
foreach($cms_arr as $cms){
$url = $this->getUrl($cms["value"]);
$cms_pages[$url] = $cms["label"];
}

Related

How to set filter in grid of admin in magento?

I am working on magento 1.7 version.
I have a grid in magento admin. When I click on a particular row it opens a form and two tabs in left sidebar.
when I click on one tab it is displaying a grid in right side.
Now I want that in this grid a filter should be auto selected.
Ex.-
http://d.pr/i/UuB4
http://d.pr/i/BN1N
In this, category should be auto selected in filter and how to get current row id in tabs.php in _beforeToHtml().
I am using following code for tabs
protected function _beforeToHtml() {
$this->addTab('form_section', array(
'label' => Mage::helper('test')->__('Category'),
'title' => Mage::helper('test')->__('Category'),
'content' => $this->getLayout()->createBlock('test/adminhtml_category_edit_tab_form')->toHtml(),
));
$this->addTab('tab_section', array(
'label' => Mage::helper('test')->__('Images'),
'title' => Mage::helper('test')->__('Images'),
'content' => $this->getLayout()->createBlock('test/adminhtml_book_grid')
->toHtml()
));
return parent::_beforeToHtml();
}
any help would be much appreciated.
you can use this code to set the filter value -
$this->setDefaultFilter(array('category'=>3));
where 3 - category id in your grid's _prepareCollection() method.

Allow empty cms page content

When using a cms page in magento I sometimes need an empty content section. Most times this is for my homepage. But magento forces me to put something in content before it can be saved.
Is there a way to get magento to allow empty cms page content?
You can use an empty div or span
The Mage_Adminhtml_Block_Cms_Page_Edit_Tab_Content::_prepareForm() method dispatches the adminhtml_cms_page_edit_tab_content_prepare_form event. You can observe this event, grab the field from the form object which is passed into the event, and change its required property to false.
This is a quick and dirty fix, you should really override the admin class so you won't lose the change when you next upgrade.
Anyways, in file app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit/Tab/Content.php, in function _prepareForm(), line 82, change:
$contentField = $fieldset->addField('content', 'editor', array(
'name' => 'content',
'style' => 'height:36em;',
'required' => true,
'disabled' => $isElementDisabled,
'config' => $wysiwygConfig
));
to
$contentField = $fieldset->addField('content', 'editor', array(
'name' => 'content',
'style' => 'height:36em;',
'required' => false,
'disabled' => $isElementDisabled,
'config' => $wysiwygConfig
));
add <div>‍</div> inside your empty elements to stop magento cms from removing them
Its not particularly elegant, but you can just enter and/or hide the content via CSS

Change Customer to Company on Magento Administrator Dashboard

On the Magento administrator Dashboard I'm looking to update the 'Last 5 Orders' block so that instead of displaying the customer name, it shows the company billing name instead.
My understanding is that this can't be done using the standard reports collection, I've seen examples for doing this on the main sales grid screen, but not on the dashboard. To add further complexity this is for the latest version of Magento 1.6.1.0 and it appears the method of doing this may have changed somewhere around 1.4.
The file I believe needs editing is:
app/code/core/Mage/Adminhtml/Block/Dashboard/Orders/Grid.php
Hopefully this is one of those 'easy when you know how' solutions that lots of people can benefit from.
Here is the code
PS : I haven't got time to test this code block.
The code block available in the :
app\code\core\Mage\Adminhtml\Block\Dashboard\Tab\Customers\Most.php
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => $this->__('Customer Name'),
'sortable' => false,
'index' => 'name'
));
$this->addColumn('orders_count', array(
'header' => $this->__('Number of Orders'),
'sortable' => false,
'index' => 'orders_count',
'type' => 'number'
));
and here is the sample link that you can override the magento admin sales grid
Override Admin Sales Order Search Grid

How to show Sum of Two fields in Grid in Magento Admin Panel

I am working on a extension in which user enter different price for " Stamp Cost, Ink Cost,
Form Cost ". currently in data grid i am showing value of one field
$this->addColumn('stamp_cost', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
'width' => '100px',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'stamp_cost'
));
But Now I Need to show sum of all these fields in one column
How can we show sum of two fields in one column in magento admin data grid ?
Essentially, there are two ways to do it. Add the field to the collection and get the data from the database, or calculate it in PHP based on the 3 values returned from the DB. Doing the first way with the Magento Collection would, in my opinion, be too complex. instead, you want to use a Renderer (Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract)
First, inside of the Block/Adminhtml folder of your plugin, make a new folder called Renderer. Inside of it make a new file called CostSum.php with the following contents:
<?php
class Company_Module_Block_Adminhtml_Renderer_CostSum extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
return $row->getStampCost() + $row->getInkCost() + $row->getFormCost();
}
}
Then, in the grid, make a new column
$this->addColumn('cost_total', array(
'header' => Mage::helper('imprint')->__('Stamp Cost'),
//'index' => 'Im not sure this is necessary',
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'renderer' => new Company_Module_Block_Adminhtml_Renderer_CostSum()
));
Hope that helps!
The more right way is 'renderer' => 'company_module/adminhtml_renderer_costSum'
As #Zyava says, the correct option is this. But actually, is not 'company_module'. Instead you should call it as you have declared your blocks in the config.xml file.
<blocks>
<declaration>
<class>Company_Module_Block</class>
</declaration>
</blocks>
So, in this case you should create the 'renderer' as:
'renderer' => 'declaration/adminhtml_renderer_costSum'

Adding Stock Status column to Manage Product Admin Page

I'm trying to add 'stock status' column to the Admin Manage Product Grid.
Stock status is either "In Stock" or "Out of Stock".
Seems like I need to edit Adminhtml/Block/Catalog/Product/Grid.php 's _prepareColumns().
I added this line
$this->addColumn('stock',
array(
'header'=> Mage::helper('catalog')->__('Stock Avail.'),
'width' => '70px',
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('cataloginventory/source_stock')->toOptionArray()
which just prints out Array,Array.
I'm guessing it's just printing out the type, so I would need to access the array value to get options. Am I on the right path? I can't find any good coding docs for magento, if anyone can share with me how they figured out magento, that would be really nice.
You should use a renderer: in the array of the addColumn, add:
'renderer' => 'YourNamespace_YourModule_Path_To_Renderer_File',
And the renderer file would be something like:
class YourNamespace_YourModule_Path_To_Renderer_File extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
//let's see what you have to work with
Zend_Debug::dump($row->getData(), 'debug');
$stockStatus = $row->getSomething();
return $stockStatus;
}
}
Let me know if that ain't clear

Resources