Magento自定义模块 - 如何添加包含父类别的自定义选择框

时间:2010-11-15 20:35:06

标签: magento

我写了一个模块来拥有自己的菜单栏,而不是仅仅使用类别作为菜单栏。

所以,在这里,我想在自定义菜单模块的Tab / Form.php中添加新菜单时显示已添加的菜单。如何将所有现有菜单名称显示为表单上的下拉列表/选项列表。这是我过去使用菜单表单的代码。

 $form = new Varien_Data_Form();
      $this->setForm($form);
      $fieldset = $form->addFieldset('menu_form', array('legend'=>Mage::helper('menu')->__('Menu information')));

  $note = "Name of this Menu";
  $fieldset->addField('title', 'text', array(
      'label'     => Mage::helper('menu')->__('Menu Name'),
      'class'     => 'required-entry',
      'required'  => true,
      'note'      => $note,
      'name'      => 'title',
  ));


  $note = "Menu level";
  $fieldset->addField('level', 'select', array(
      'label'     => Mage::helper('menu')->__('Menu level'),
      'name'      => 'level',
      'note'      => $note,
      'values'    => array(
          array(
              'value'     => 1,
              'label'     => Mage::helper('menu')->__('Level 1'),
          ),

          array(
              'value'     => 2,
              'label'     => Mage::helper('menu')->__('Level 2'),
          ),
      ),
  ));

  $model = Mage::registry('menu');
  $fieldset->addField('parent', 'text', array(
        'name' => 'conditions',
        'label' => Mage::helper('menu')->__('Parent Menu'),
        'title' => Mage::helper('menu')->__('Parent Menu'),
        'required' => false,
        'note'      => $note,
  ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));

 $fieldset->addField('target', 'select', array(
        'label'     => Mage::helper('menu')->__('Open in new window'),
        'name'      => 'target',
        'values'    => array(
            array(
                'value'     => "_blank",
                'label'     => Mage::helper('menu')->__('Yes'),
            ),

            array(
                'value'     => "_self",
                'label'     => Mage::helper('menu')->__('No'),
            ),
        ),
    ));

    $fieldset->addField('status', 'select', array(
        'label'     => Mage::helper('menu')->__('Status'),
        'name'      => 'status',
        'values'    => array(
          array(
              'value'     => 1,
              'label'     => Mage::helper('menu')->__('Enabled'),
          ),

          array(
              'value'     => 2,
              'label'     => Mage::helper('menu')->__('Disabled'),
          ),
        ),
    ));


  $note = "Menu Links to Which page. BaseURL(<b>".str_ireplace("index.php/","",Mage::getBaseUrl())."</b>) Will be Added Dynamically, Please add Your new page Refrence alone";
  $fieldset->addField('menulink', 'text', array(
      'label'     => Mage::helper('menu')->__('URL'),
      'required'  => true,
      'class'     => 'required-entry',
      'note'      => $note,
      'name'      => 'menulink',
  ));


  $fieldset->addField('position', 'select', array(
        'label'     => Mage::helper('menu')->__('Position'),
        'name'      => 'position',
        'values'    => array(
          array(
              'value'     => 1,
              'label'     => Mage::helper('menu')->__('Top 1'),
          ),

          array(
              'value'     => 2,
              'label'     => Mage::helper('menu')->__('Top 2'),
          ),
        ),
  ));



  if ( Mage::getSingleton('adminhtml/session')->getMenuData() )
  {
      $form->setValues(Mage::getSingleton('adminhtml/session')->getMenuData());
      Mage::getSingleton('adminhtml/session')->setMenuData(null);
  } elseif ( Mage::registry('menu_data') ) {
      $form->setValues(Mage::registry('menu_data')->getData());
  }
  return parent::_prepareForm();

在此,我想在父菜单选项下显示所有添加的菜单。我应该在我的Model类中编写什么内容,以便我可以有一个下拉列表来显示所有内容,添加后应该添加到数据库中。

请帮助我,我在这里挣扎。

1 个答案:

答案 0 :(得分:8)

您可以通过添加这些行来完成此操作。

它会将已添加的菜单显示为下拉列表,以便您可以选择其中任何一个作为当前项目的父菜单。

      $_menus = Mage::getSingleton('menus/menus')->getCollection(); 
      foreach($_menus as $item)
  { 
        if($item->getParent == NULL){
            $_menuItems[] = array(
                        'value'     => $item->getId(),
                        'label'     => $item->getTitle(),
                    );
        }
  }

  $note = "Choose the parent menus for this item";
  $fieldset->addField('parent', 'select', array(
        'name'      => 'parent',
        'label'     => Mage::helper('menus')->__('Parent Menu'),
        'title'     => Mage::helper('menus')->__('Parent Menu'),
        'required'  => false,
        'note'      => $note,
        'class'     => 'HideIt',
        'values'    => $_menuItems,
  ));