dsxfa26482 2019-01-17 23:52
浏览 106
已采纳

Drupal 8 - MenuLinkContent通过loadByProperties获取所有孩子

I need help to get all MenuLinkContent children from a specific MenuLinkContent and can't find a solution. I tried several different ways, but no success at all.

Here goes my code:

//get all MenuLinkContent published with name 'myname'
$main_menu = \Drupal::entityTypeManager()->getStorage('menu_link_content')
      ->loadByProperties(['menu_name' => 'myname' , 'enabled' => 1]);

foreach ($main_menu as $menu) {
  //could not find a better solution, so i have to check if parent is empty.
  if ($menu->getParentId()=='') {
    //here i'm triyng to get all children, 
    $child_menu = \Drupal::entityTypeManager()->getStorage('menu_link_content')
          ->loadByProperties(['menu_name' => 'myname',  'parent' => $menu ]); 

As you can see the property 'parent' do not fit with '$menu'. I tried several different properties from '$menu' and no one seems to match my query.

If more information is needed, just ask and i will post here. Other ways to achieve this iteration are welcome too.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongwu5318 2019-01-18 12:56
    关注

    Got it, found this code from @Slim and works fine:

    Below code was taken from this answer https://drupal.stackexchange.com/a/224786/89808

    "I'm quite late, but maybe helps someone looking for answers, here's my solution for generating recursive array from menu items." @Slim

    private function generateSubMenuTree(&$output, &$input, $parent = FALSE) {
    $input = array_values($input);
    foreach($input as $key => $item) {
      //If menu element disabled skip this branch
      if ($item->link->isEnabled()) {
        $key = 'submenu-' . $key;
        $name = $item->link->getTitle();
        $url = $item->link->getUrlObject();
        $url_string = $url->toString();
    
        //If not root element, add as child
        if ($parent === FALSE) {
          $output[$key] = [
            'name' => $name,
            'tid' => $key,
            'url_str' => $url_string
          ];
        } else {
          $parent = 'submenu-' . $parent;
          $output['child'][$key] = [
            'name' => $name,
            'tid' => $key,
            'url_str' => $url_string
          ];
        }
    
        if ($item->hasChildren) {
          if ($item->depth == 1) {
            $this->generateSubMenuTree($output[$key], $item->subtree, $key);
          } else {
            $this->generateSubMenuTree($output['child'][$key], $item->subtree, $key);
          }
        }
      }
    }
    

    And call that function with

    //Get drupal menu
    $sub_nav = \Drupal::menuTree()->load('sub-navigation', new \Drupal\Core\Menu\MenuTreeParameters());
    //Generate array
    $this->generateSubMenuTree($menu_tree2, $sub_nav);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题