Jerry Abraham
Jerry Abraham

Reputation: 1039

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

I created a module in which it will hook to one of the pages. But how to display the Link of my custom module to the top horizontal menu in Prestashop 1.6. i.e when I Navigate to Modules -> Modules and looking up for Top horizontal menu. and when I Click the Configure button to access the module configuration page how can I see my module link in the available Items lists. i.e see the image in the Available Items I want my custom link. Link of my custom module which i developed . i.e when my custom module is installed the link must appear here in the Available Items ,So that I can add my custom link the selected item enter image description here

Upvotes: 1

Views: 1605

Answers (1)

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

You have two choices:

  • create it by hand in the backoffice

    In the blocktopmenu module configuration page you can create a new custom link under ADD A NEW LINK block. The link to your module will be /module/your_module_name/your_controller_name. Then you will be able to add this link to your menu under MENU TOP LINK.

  • create it programmatically

    In the install method of your module you can create this custom link using blocktopmenu methods.

    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
    
        }
    }
    
  • create it programmatically with auto-suppression

    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);
                }
            }
        }
    }
    

    Code no tested but must be working with Prestashop 1.6.

Upvotes: 2

Related Questions