Simple menu in custom module

Hey all :waving_hand:

I’m trying to use the simple menu field type in a custom module to create a customisable link. I’m having trouble when it comes to handling the different types of menu items. For example the URL Link returns the value of the field to

linkUrl

, but if the menu item is a Page link with URL parameter the

linkUrl

returns

null

and I can’t see any other fields on the object that I can use to get the url of the page.

This is what I’m working with:

<a href="{{ module.button.link[0].linkUrl }}{{ module.button.link[0].linkParams }}" class="{{ module.button.type }} button--block">
  {{ module.button.link[0] }}
</a>

Ideally what I need to be able to do is change the href of the link to URL defined in the menu item, regardless of the type of the menu item. Any idea how I can acheive this? I’m thinking that I must be missing something…

Ok, I think I’ve figured it out. I can use the simple menu field (in this case link) to get the desired URL string. Just in case anyone else is wondering…

If the menu item is a URL Link I can just use the linkUrl value. Easy.

{{ module.button.link[0].linkUrl }}

If the menu item is a Page link with URL parameter I need to use the page_by_id() function to get the absolute URL of the specified page by passing it the pageLinkId. :raising_hands:

{{ page_by_id(module.button.link[0].pageLinkId).absolute_url }}

If there’s a better/easier way to do this, I’m all ears!