如何为我的自定义模块页面添加一个新的 link 到 Prestashop 1.6.4 的顶部水平菜单

How to add a new link for my custom module page to the top horizontal menu in Prestashop 1.6.4

我创建了一个模块,它将挂接到其中一个页面。但是如何在 Prestashop 1.6 中将我的自定义模块的 Link 显示到顶部水平菜单。即当我导航到模块 -> 模块并查找顶部水平菜单时。当我单击“配置”按钮访问模块配置页面时,如何在可用项目列表中看到我的模块 link。即在我想要我的自定义 link 的可用项目中查看图像。 Link 我开发的自定义模块。即当我的自定义模块安装时 link 必须出现在可用项目中,这样我就可以添加我的自定义 link 所选项目

您有两个选择:

  • 在后台手动创建

    在 blocktopmenu 模块配置页面中,您可以在 ADD A NEW LINK 块下创建一个新的自定义 link。您的模块的 link 将是 /module/your_module_name/your_controller_name。然后您就可以将此 link 添加到 MENU TOP LINK 下的菜单中。

  • 以编程方式创建它

    在模块的安装方法中,您可以使用 blocktopmenu 方法创建此自定义 link。

    if (Module::isInstalled("blocktopmenu"))
    {
        // You will have to put the right path in here
        require_once('../blocktopmenu/menutoplinks.class.php');
    
        $languages = $this->context->controller->getLanguages();
        $shops = Shop::getContextListShopID();
    
        $links_label = array();
        $labels = array();
    
        foreach ($languages as $key => $val)
        {
            // You need to replace "my_module" and "my_controller" to get a link to your controller
            $links_label[$val['id_lang']] = Context::getContext()->link->getModuleLink("my_module", "my_controller");
            // Here set your link label for the menu
            $labels[$val['id_lang']] = "My Link Name";
        }
    
        foreach ($shops as $shop_id)
        {
            $added = MenuTopLinks::add($links_label, $labels, 0, (int) $shop_id);
            // You can check wether $added is true or false
    
        }
    }
    
  • 通过自动抑制以编程方式创建它

    public function install()
    {
        if (! parent::install())
        {
            return false;
        }
    
        if (Module::isInstalled("blocktopmenu"))
        {
            // You will have to put the right path in here
            require_once('../blocktopmenu/menutoplinks.class.php');
    
            $languages = $this->context->controller->getLanguages();
            $shops = Shop::getContextListShopID();
    
            foreach ($shops as $shop_id)
            {
    
                $links_label = array();
                $labels = array();
    
                foreach ($languages as $key => $val)
                {
                    // You need to replace "my_module" and "my_controller" to get a link to your controller
                    $links_label[$val['id_lang']] = Context::getContext()->link->getModuleLink("my_module", "my_controller", array(), null, $val['id_lang'], $shop_id);
                    // Here set your link label for the menu
                    $labels[$val['id_lang']] = "My Link Name";
                }
    
                $added = MenuTopLinks::add($links_label, $labels, 0, (int) $shop_id);
    
                if ($added) {
                    $link_id = Db::getInstance()->getValue("
                        SELECT DISTINCT id_linksmenutop
                        FROM `"._DB_PREFIX_."linksmenutop_lang`
                        WHERE link LIKE '" . $link . "'
                        AND id_shop LIKE '" . $shop_id . "'
                    ");
    
                    Configuration::set("MY_MODULE_LINKS_TOP_" . $shop_id, $link_id);
                }
            }
        }
    }
    
    public function uninstall()
    {
        if (! parent::uninstall())
        {
            return false;
        }
    
        if (Module::isInstalled("blocktopmenu"))
        {
            // You will have to put the right path in here
            require_once('../blocktopmenu/menutoplinks.class.php');
    
            $shops = Shop::getContextListShopID();
    
            foreach ($shops as $shop_id)
            {
                $link_id = Configuration::get("MY_MODULE_LINKS_TOP_" . $shop_id);
    
                if ($link_id !== false)
                {
                    MenuTopLinks::remove($link_id, $shop_id);
                }
            }
        }
    }
    

    代码未经测试,但必须与 Prestashop 1.6 一起使用。