Clickable menu dropdown titles



  • Hi !

    I need clickable parent menu items, but if I try to use $item->getUrl then i got an URLgenerator instance instead of an url.. In menuItem object there isnt 'url' attribute if I dump it in my menupresenter. getMenuWithDropDownWrapper($item) function.

    public function getMenuWithDropDownWrapper($item)
    {
        dump($item->getUrl());//its not an url but an URLgenerator instance if the menu has childs
    

    ...

    In my case I need a menutree with someextra data (which is in the database, like ID of menuItem) in my controller too (on pages what have sub pages I have to put some excerpted cntent from sub pages)

    Can Anybody help to get that data for menuItem what has childs to make clickable the parent menuitems?


  • admin

    Hello,

    Did you check the UrlGenerator inside laravel? I see it has a path method for example.



  • Hi, thanks for quick reaction!

    Yes I checked, but I think I dont need that. I need the url attribute from database for the passed item to the function ($item)
    I dumped this:
    public function getMenuWithDropDownWrapper($item)
    {
    dump(new \ReflectionClass($item->getUrl()));
    ...
    result:

    ReflectionClass {#1210 ▼
    +name: "Illuminate\Routing\UrlGenerator"
    implements: array:1 [▼
    0 => "Illuminate\Contracts\Routing\UrlGenerator"
    ]
    constants: []
    properties: array:12 [▶]
    methods: array:38 [▼
    "__construct" => ReflectionMethod {#1253 ▶}
    "full" => ReflectionMethod {#1254 ▶}
    "current" => ReflectionMethod {#1255 ▶}
    "previous" => ReflectionMethod {#1256 ▶}
    "getPreviousUrlFromSession" => ReflectionMethod {#1257 ▶}
    "to" => ReflectionMethod {#1258 ▶}
    "secure" => ReflectionMethod {#1259 ▶}
    "asset" => ReflectionMethod {#1260 ▶}
    "secureAsset" => ReflectionMethod {#1261 ▶}
    "assetFrom" => ReflectionMethod {#1262 ▶}
    "removeIndex" => ReflectionMethod {#1263 ▶}
    "formatScheme" => ReflectionMethod {#1264 ▶}
    "route" => ReflectionMethod {#1265 ▶}
    "toRoute" => ReflectionMethod {#1266 ▶}
    "action" => ReflectionMethod {#1267 ▶}
    "formatAction" => ReflectionMethod {#1268 ▶}
    "formatParameters" => ReflectionMethod {#1269 ▶}
    "extractQueryString" => ReflectionMethod {#1270 ▶}
    "formatRoot" => ReflectionMethod {#1271 ▶}
    "format" => ReflectionMethod {#1272 ▶}
    "isValidUrl" => ReflectionMethod {#1273 ▶}
    "routeUrl" => ReflectionMethod {#1274 ▶}
    "defaults" => ReflectionMethod {#1275 ▶}
    "forceScheme" => ReflectionMethod {#1276 ▶}
    "forceRootUrl" => ReflectionMethod {#1277 ▶}
    "formatHostUsing" => ReflectionMethod {#1278 ▶}
    "formatPathUsing" => ReflectionMethod {#1279 ▶}
    "pathFormatter" => ReflectionMethod {#1280 ▶}
    "getRequest" => ReflectionMethod {#1281 ▶}
    "setRequest" => ReflectionMethod {#1282 ▶}
    "setRoutes" => ReflectionMethod {#1283 ▶}
    "getSession" => ReflectionMethod {#1284 ▶}
    "setSessionResolver" => ReflectionMethod {#1285 ▶}
    "setRootControllerNamespace" => ReflectionMethod {#1286 ▶}
    "macro" => ReflectionMethod {#1287 ▶}
    "hasMacro" => ReflectionMethod {#1288 ▶}
    "__callStatic" => ReflectionMethod {#1289 ▶}
    "__call" => ReflectionMethod {#1290 ▶}
    ]
    extra: {▶}
    }
    the item dump to array is:
    public function getMenuWithDropDownWrapper($item)
    {
    dump( $item);
    ...
    result:
    MenuItem {#653 ▼
    #properties: array:4 [▼
    "title" => "Rólunk"
    "order" => 0
    "attributes" => array:2 [▼
    "target" => "_self"
    "class" => ""
    ]
    "icon" => ""
    ]
    #childs: array:3 [▶]
    #fillable: array:10 [▶]
    #hideWhen: null
    +"title": "Rólunk"
    +"order": 0
    +"attributes": array:2 [▼
    "target" => "_self"
    "class" => ""
    ]
    +"icon": ""
    }
    there is no url attribute or property if the menuitem has childs...

    I need that property for linking those menu items which have childs.



  • Found a solution:

    modified: Modules\Menu\Providers\MenuServiceProvider.php

      public function addItemToMenu(Menuitem $item, Builder $menu)
    {
        if ($this->hasChildren($item)) {
            $url = $item->link_type != 'external' ? $item->locale . '/' . $item->uri : $item->url;
    
            $this->addChildrenToMenu(
                $item->title,
                $item->items,
                $menu,
                [
                    'icon' => $item->icon,
                    'target' => $item->target,
                    'class' => $item->class,
                    'href' => $url,
                ]
            );
        } else {...
    

    then in my menupresenter easily make links from parent menus like this:

    public function getMenuWithDropDownWrapper($item)
    {
    
        return '<li class="menu-item">
                    <a '.$item->getAttributes().'>
                        ' . $item->getIcon() . ' ' . $item->title . '
                    </a>
                    <div class="megamenu">
                        <div class="megamenu-row">
                                <ul class="list-unstyled list-dashed">'
                            . $this->getChildMenuItems($item) . '
                                </ul>
                        </div>
                     </div>
                </li>' . PHP_EOL;
    }
    

    Now the main menu items are clickable!!!! :)

    Cheers!


Log in to reply
 

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.