Magento Custom Module: Displaying Existing Menus as a Select Box
Hello! In this post, we will guide you through displaying existing menus from your custom module as a select box (dropdown list) in the form for adding new menus in Magento. We’ll build upon the code you’ve shared and provide step-by-step instructions to achieve this.
The Current Scenario
You have already created a menu form with fields like title, level, status, etc., using Varien_Data_Form. You want to display all the existing menu names as options in a dropdown list under the ‘Parent Menu’ field, and then save the selected parent menu ID along with other details in the database.
The Solution: Fetching Menu Data in the Model
To accomplish this, we need to fetch all the existing menus from the database and pass them as an array of options for the parent select field in your form. Here’s how you can do it in your model class (assuming you have a table named ‘menu’ with fields like menu_id, title, etc.):
public function getMenus()
{
$menus = Mage::getModel('your_module/your_model')->getCollection();
$options = array();
foreach ($menus as $menu) {
$options[$menu->getMenuId()] = $menu->getTitle();
}
return $options;
}
In this code, replace your_module and your_model with the actual module name and model name you’re using. This method fetches all the menus from the database and creates an associative array where the keys are menu IDs and values are menu titles.
Integrating Menu Options in the Form
Now, let’s modify your form generation code to use this new method:
$note = "Parent Menu";
$fieldset->addField('parent', 'select', array(
'name' => 'parent_id',
'label' => Mage::helper('menu')->__('Parent Menu'),
'note' => $note,
'values' => $this->getMenus(), // Use the method we created
));
We’ve changed the name to parent_id, which matches the database field name, and used getMenus() to fetch menu options. The values array will now contain all existing menu titles as options for the select field.
Saving the Selected Parent Menu ID
When the form is submitted, you need to save the selected parent menu’s ID instead of its title. To do this, modify your controller’s action that handles form submission:
if ($data = $this->getRequest()->getPost()) {
// ...
$parentMenuId = (int)$data['parent_id']; // Get the selected parent menu ID
$model->setData('title', $data['title']);
$model->setData('level', $data['level']);
$model->setData('status', $data['status']);
$model->setData('menulink', $data['menulink']);
$model->setData('position', $data['position']);
$model->setData('parent_id', $parentMenuId); // Set the parent menu ID
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('menu')->__('Your menu has been saved!'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
// ...
}
}
In this code, we’re fetching the selected parent menu ID from the submitted data using $data['parent_id'], converting it to an integer with (int), and then setting that value for the parent_id database field.
Conclusion
That’s it! You’ve successfully added a select box containing all existing menus as options in your custom menu form. When you add a new menu, you can now choose its parent from this dropdown list. The selected parent menu ID will be saved along with other details when the form is submitted.
Remember to replace your_module and your_model in the code examples with the actual names of your module and model. Also, ensure that your database table and fields are correctly defined according to the example queries used here.
Happy coding! If you need any further assistance or have other Magento development questions, feel free to check out our Magento Development Services at Laravel Company.
Thank you for reading!