【问题标题】:How to add a new link for my custom module page to the top horizontal menu in Prestashop 1.6.4如何将我的自定义模块页面的新链接添加到 Prestashop 1.6.4 的顶部水平菜单
【发布时间】:2016-04-28 06:16:49
【问题描述】:

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

【问题讨论】:

    标签: prestashop prestashop-1.6


    【解决方案1】:

    你有两个选择:

    • 在后台手动创建

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

    • 以编程方式创建它

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

      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。

    【讨论】:

    • 好的,它工作正常,我正确设置了路径 require_once('/../blocktopmenu/menutoplinks.class.php');但是当我从 BO 卸载我的模块时,链接没有被删除那么如何删除这个我是否还需要在卸载中添加一些东西? @Florian Lemaitre
    • 是的,在您的卸载方法中使用MenuTopLinks::remove($id_linksmenutop, $id_shop) 删除您的链接
    • 嗨@JerryAbraham 你不介意接受这个答案来将此线程标记为已解决吗?或者您需要更多信息吗?
    • 是的,我将很快标记接受另外一个信息,我需要的是,当我卸载模块时,我将如何删除链接?
    • 我添加了一个卸载示例。这不是一个干净的解决方案,因为MenuTopLinks::add() 不会返回新添加的元素的 id。所以我们需要查询数据库来获取。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多