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

发布于 2024-10-02 18:39:58 字数 3984 浏览 2 评论 0原文

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

因此,在这里我想显示已添加的菜单,同时在自定义菜单模块的 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();

在此我想显示父菜单选项下所有添加的菜单。我应该在我的模型类中编写什么,以便我可以有一个下拉列表来显示所有它们,并且添加后应该将其添加到数据库中。

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

I wrote a module to have my own menu bar, rather than just using categories as a menu bar.

So, here I want to show already added menus while adding new menu in the Tab/Form.php in my custom menu module. How can I show all of the existing menu names as a dropdown/options list on the form. Here is the code that I used to have menu form.

 $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();

In this I want to show all the added menus under the parent menu option. What should I write in my Model class so that I can have a drop down list to show them all, and after adding it should be added to database.

Please help me, am struggling here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2024-10-09 18:39:58

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

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

      $_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,
  ));

You can do this by adding these lines.

It will show already added menus as a dropdown list so that you can choose any of them as a parent menu for current item.

      $_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,
  ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文