Blog» ezurl() links in eZ Publish 5

ezurl() links in eZ Publish 5

By Peter Keung  | August 20, 2014  |  eZ Publish development tips

In eZ Publish 4 / legacy, formatting link URLs is handled by the well-known ezurl() template operator. This is especially useful when you have multiple siteaccesses and you use URL-based matching. In eZ Publish 5, there is no single ezurl() equivalent; instead, there are several options depending on the type of link you want to display.

In eZ Publish legacy, you can pass any string to ezurl() and it will handle leading slashes and prepend the siteaccess name if necessary.  You could pass a URL alias or any custom path:

{$node.url_alias|ezurl()}
{'my_module/my_view/(param1)/value1'|ezurl()}
{$search_result.url_alias|ezurl()}
{$external_path|ezurl()}

eZ Publish 5 is more strict, as it tries to validate the type of link you want to create.  It uses the path Twig function but its usage depends on the type of destination link.

To link to a location/node object, you use the object directly:

{{ path( location_object ) }}

If you have a location/node ID only, you must pass in "ez_urlalias" as the first argument and then supply the location ID:

{{ path( 'ez_urlalias', {'locationId': content.contentInfo.mainLocationId} ) }}

If you are linking to a legacy module view, you must pass in "ez_legacy" as the first argument and then supply the module path:

{{ path( 'ez_legacy', { 'module_uri': 'my_module/my_view/(param1)/value1' } ) }}

If you are linking to a Symfony route, you must pass in the route identifier (not the route path) and then any required or desired parameters:

{{ path( 'job_application', { 'jobID': 55 } ) }}

Since an arbitrary path string is not supported, there are at least a couple of non-covered edge cases:

  • If you're linking to a page outside of the content management system but you still need to preserve the siteaccess name in the URL
  • If you have a search result path (not a Location object)

One way to solve this is to create your own Twig function. See the Twig documentation on the steps to set up custom functions. Here is an example of the important functionality for an ezurl()-like Twig function in eZ Publish 5. Note that it is only intended to cover the case where you are using URL-based matching.

public function ezurlFunctions($url)
{
    $currentSiteaccess = $this->globalHelper->getSiteaccess();
    $siteAccessName = $currentSiteaccess->name;
 
    // Inject the siteaccess name if we are using URI matching and if we are not in the default siteaccess
    // ezpublish.siteaccess.default maps to the actual setting ezpublish.siteaccess.default_siteaccess
    if( $siteAccessName != $this->container->getParameter( 'ezpublish.siteaccess.default' ) )
    {
        $matcherName = $currentSiteaccess->matcher->getName();
        if( $matcherName == 'uri:map' )
        {
            $url = $currentSiteaccess->matcher->analyseLink( $url );
        }
    }
    return $url;
}

You can then use that Twig function similarly to how you would use the legacy ezurl() template operator:

{{ '/path/to/page'|ezurl }}

Join our mailing list for news, events, tips, and tools

Related Blog Posts

Cross-siteaccess internal links in eZ Publish

Among the many advantages of hosting multiple sites on the same eZ Publish installation is the ability to create internal links. eznode:// and ezobject://...

Read more »

Version history limit in eZ Publish 5

Whenever you edit content in the eZ Publish Administration Interface, eZ Publish stores a new version. eZ Publish has a built-in feature to limit the...

Read more »

An introduction to fetching content in eZ Publish 4 and eZ Publish 5

Fetching content in eZ Publish 4 (the "legacy" stack) and eZ Publish 5 (the "new" stack) follow many of the same principles, which is not surprising since...

Read more »

Comments

blog comments powered by Disqus

Hi, we're Mugo Web - Nice to meet you!

We're a group of web experts who solve complex web problems.

Learn more about us »

Search


Categories


Yes - we can do that.

Many years of experience with complex websites allows us to offer total solutions.

Learn more about what we can do »

We love our clients (and they love us too)

We've solved problems across North America and around the world.

Learn more about what we've done »

We tweet too

Follow us on Twitter for the latest Mugo happenings

mugo twitter page @mugo

© 2008 - 2019 Mugo Web. All rights reserved.