Say I want to add a link to page node/1
in a block in the footer. I want the link to work for all these conditions…
http://localhost/site1/web/
– the site home page
http://localhost/site1/web/admin/
– when I’m on a page that has a different path
http://localhost/site2/web/
– when I clone the site
http://mysite.com/
– on the live server
I can’t use relative paths (node/1
) as this wont work when the current page is on a different path.
I can’t use a server relative path. E.g. /site1/web/node/1
only works when on localhost/site1
And similar for absolute paths (http://....
).
I tried the module linkit. It works for all the above cases but only for some types of links (e.g. I couldn’t get it to work for links to views).
I got the best results using the php module (it supplies a filter that you can add to a text-format) …
<?php use DrupalCoreLink; use DrupalCoreUrl; print(Url::fromUserInput('/node/1')->toString()); ?>
But I believe using this module is frowned upon.
Am I missing a better alternative? (It would be handy if there was a way to make links that are relative to the drupal ‘web’ directory.)