External link in menu with custom attributes

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
comecloser
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Oct 27, 2020 8:32 pm

External link in menu with custom attributes

Post by comecloser » Tue Oct 27, 2020 8:42 pm

Hi,

I have javascript code and need to put link in menu to execute it, something like:

Code: Select all

'<a href="#" class="customclass" data-appname="sth" data-token="sth" role="button" id="myid" style="background-image:none;">MY CUSTOM LINK</a>
Any ideas how to achieve that in Joomla 3.9? External link in Joomla's menu doesn't allow to put custom attributes that are necessary to execute script in proper way.

Thanks in advance :)

User avatar
AMurray
Joomla! Champion
Joomla! Champion
Posts: 6208
Joined: Sat Feb 13, 2010 7:35 am
Location: Australia

Re: External link in menu with custom attributes

Post by AMurray » Tue Oct 27, 2020 9:59 pm

If joomla doesn't allow it then it doesn't seem possible. Unless I'm missing something.....I don't see any javascript in that sample code you've given.

I actually don't understand what you're trying to do - execute a script in a menu item...?
Regards,
--------------------------------------------------------------
A Murray
Help you I can, yes!. Post your question, you should. Keep it on topic you must!
Use the Forc....Forum Post Assistant my young Padawan!

annahersh
Joomla! Guru
Joomla! Guru
Posts: 713
Joined: Wed Aug 15, 2018 8:23 pm

Re: External link in menu with custom attributes

Post by annahersh » Wed Oct 28, 2020 5:39 am

comecloser wrote:
Tue Oct 27, 2020 8:42 pm
I have javascript code and need to put link in menu to execute it, something like:

Code: Select all

'<a href="#" class="customclass" data-appname="sth" data-token="sth" role="button" id="myid" style="background-image:none;">MY CUSTOM LINK</a>
Assuming you know how to use Joomla's template override system, you can simply create a template override of mod_menu/tmpl/default_url.php and add an array process to insert all the attributes your desire.

Add the following just before the last line echo JHtml

Code: Select all

// check the saved URL string for a comma and run this process, else do default
if( strstr($item->flink, ',') ) 
{
  // create an array and assign variables to each element
  list($link, $class, $dataapp, $datatoken, $role, $id) = explode(',',$item->flink);
  // set the core link value to void and strip the slash
  $item->flink = 'javascript:;';
  $item->flink = str_replace('/', '', $item->flink);
  // set the core attribute value to the custom values
  $attributes = 'id="'.$id.'" class="'.$class.'" data-appname="'.$dataapp.'" data-token="'.$datatoken.'" role="'.$role.'"';
}
Setting the menu item
- Choose System Links > URL
- Insert the 6 values and separate each with a comma
menu-item-url.jpg
The resulting item
link-result.jpg
You do not have the required permissions to view the files attached to this post.

comecloser
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Oct 27, 2020 8:32 pm

Re: External link in menu with custom attributes

Post by comecloser » Wed Oct 28, 2020 6:13 pm

Hi Anna,

I've just followed your steps, but it looks that something went wrong.

I'm using external template, so I've changed default_url.php file in templates corresponding path templates/template_name/html/mod_menu, but it didn't work too.

When I check html source it looks, that overriding code won't work:

Code: Select all

<li class="sp-menu-item"><a  href="/linkurl,customclass,sth,sth,button,myid" >MY CUSTOM LINK</a></li>
Perhaps the problem is connected to SP Page Builder, that template is based on?

annahersh
Joomla! Guru
Joomla! Guru
Posts: 713
Joined: Wed Aug 15, 2018 8:23 pm

Re: External link in menu with custom attributes

Post by annahersh » Wed Oct 28, 2020 9:19 pm

comecloser wrote:
Wed Oct 28, 2020 6:13 pm
I've just followed your steps, but it looks that something went wrong.
Did you use Joomla's template override generator? If all you copied was default_url.php, it's not gonna be found because the file is loaded by default.php, so you need to copy all the files in mod_menu/tmpl -- to your template override.


Post Reply

Return to “General Questions/New to Joomla! 3.x”