LinkItem: how to create a disabled / null link How do I create a LinkItem that will render into

&lt;a href="javascript:void(0)"&gt;Do nothing&lt;/a&gt;


or another form of unclickable link?

Motivation:

I am implementing a computed field of type link, which under some circumstances should become disabled. The HTML attribute disabled is not sufficient.

Code:

if ($disabled) {
  $url = 'internal:#'; # want: 'javascript:void(0)';
  $attributes = ['class' =&gt; 'btn btn-danger', 'disabled' =&gt; ''];
  $title = t('Disabled');
}

$field_item_list-&gt;createItem(0, array(
  'uri' =&gt; $url,
  'title' =&gt; $title,
  'options' =&gt; ['attributes' =&gt; $attributes],
));
A different option:


Add a class to your disabled links.
Create a JS behavior that targets a.your-class and uses preventDefault() on the click event.
Add to libraries file for your module and add library as needed.


...
  $('a.your-class').click( function (e) {
    e.preventDefault();
  });
...