In Form API when creating a link, the output a-anchor will have a prefix of "en/" in front of whatever I set as the href value and if href is set to "#" it will be output with the language prefix and # is escaped and becomes a code something like "en/%23"

this is the code:

$element['add_address'] = array(
            '#type' => 'link',
            '#title' => t('Add new address'),
            '#href' => '#',

I need that link to not have a href destination value, as it's supposed to react to javascript only. I know I could do a preventDefault() or any of the same resulting bypasses with JS, but I'm curious how to make a empty anchor with the form API.



  • 1
    Does this answer your question? How to create an EMPTY anchor-only link with l()? – leymannx Mar 7 '20 at 8:13
  • No, but Clive's comment under one of the answers did in May 2017, unfortunately it was never posted as an answer. – Alari Truuts Mar 8 '20 at 14:00
  • Could you then maybe post it as an answer and accept it to help future readers to find the right solution for sure? – leymannx Mar 8 '20 at 14:48

Not sure if this will work, give this a try

'#href' => '&num',  OR  '#href' =>'&#x00023', OR '#href' => '&#35',
  • 'twill not, the # representing numbers and & will also be escaped. Drupal doesn't have a method for adding an empty fragment believe it or not. There are a couple of hacks, I'll see if I can dig one up – Clive May 17 '16 at 9:44
  • @Clive I had a similar problem with Twig Twig: How to print special symbols, such as Copyright, Trademark, etc thought the character code solution there may work here. I guess after thinking it over, you're right. – No Sssweat May 17 '16 at 9:51
  • 1
    I think this is the canonical post @AlariTruuts: drupal.stackexchange.com/questions/51596/… – Clive May 17 '16 at 10:01
  • 1
    Errrrr...try '#href' => '', '#options' => array('fragment' => ' ', 'external' => TRUE) in the render array – Clive May 17 '16 at 10:23
  • 1
    @Clive: You're a very smart person :)! the solution works, mind if you mark it as an answer, so I can accept? it puts the href out as "# " with the following space, curious as to why that is, but works perfectly! Thanks! – Alari Truuts May 17 '16 at 20:09

I would try this to create a link:

  l(t('My Anchor Link'), '', array(
     'fragment' => ' ',        // use a space here
     'external' => TRUE,      // this will solve your problem
     'attributes' => array(
        'title' => 'Title description'
  • It would be helpful to use the same render array, which should be possible by including that array as the #options element. – mradcliffe Jan 27 '19 at 5:04

As per Clive (https://drupal.stackexchange.com/users/2800/clive) answer in the comments:

'#href' => '', '#options' => array('fragment' => ' ', 'external' => TRUE)

in the render array solves the issue and removes the pre-processing with the language prefixes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.