Cms: Provide site-specific siteUrl method

Created on 21 Feb 2018  路  2Comments  路  Source: craftcms/cms

Description

Currently creating a url a site that is _not_ the current one is a bit verbose, e.g. in Twig:

siteUrl('my/path', null, null, 1)

It would be nicer if we could call it by handle, without passing all those unused params.

Something like this in UrlHelper, also exposed to Twig could work:

    public static function siteUrlByHandle($siteHandle, string $path = '', $params = null, string $scheme = null)
    {
      $site = Craft::$app->getSites()->getSiteByHandle($siteHandle);

      return siteUrl($path, $params, $scheme, $site->id);
    }

Then one could do:

siteUrlByHandle('de', 'my/path')

鈥r this could be a method of craft\models\Site, in which case you could do:

craft.app.sites.getSiteByHandle('de').url('my/path')

Most helpful comment

Twig supports named arguments, so you can actually just do this:

{{ siteUrl('my/path', siteId=1) }}

All 2 comments

Twig supports named arguments, so you can actually just do this:

{{ siteUrl('my/path', siteId=1) }}

Named args, how luxurious. What is this ES6?!
Had no idea, thanks.

Was this page helpful?
0 / 5 - 0 ratings