When using the URL object's "toString()" function it does not return the correct url when a domain specific path (redirect) was created for the node. In other words uri's are not connected to the redirect, that was created for the node via the domain path module.

Example:
I have 3 nodes. Each of the nodes have to use the path "products". Each path does exist on each of my 3 domains.

- abc1.com/products
- abc2.com/products
- abc3.com/products

Now in my twig template I am not able to create a proper alias (as there are no aliases). What I have is just a redirect created by the domain access. So uriToString() will output "node/222". I understand why it does this. However - this is not ideal.

Also fields like the link field are not able to output the url that was created via "domain path" module. The rendered url will be the internal node/222 one - not the "/products" one.

Could we use a hook that checks the Url object on output for possible "domain access redirects" if there is no alias in place?
How to solve this issue? Using node/2673 as an url in links is no option due to google and co.

Thanks

Comments

mogio_hh created an issue. See original summary.

mogio_hh’s picture

Issue summary: View changes
mogio_hh’s picture

Issue summary: View changes
agentrickard’s picture

Domain Access does not issue redirects. Please be more precise when describing the issue. If you are seeing actual server redirects, that may be coming from another module.

Only Domain Path (which rewrites aliases per domain) and Domain Source (which rewrites the canonical URL) would affect the URL that is generated.

mogio_hh’s picture

Thanks for your reply.

I try to explain the issue in simple steps.

1.) I have 3 domains, that are created via the Domain Module.
2.) I need to have on every domain a node with the path "/products". I create 3 nodes. As I am not able to create 3 alias with the wording "products" I leave the field "alias" blank and use the domain path input. This works perfectly. Each node is accessible by typing in the path directly.

- abc1.com/products
- abc2.com/products
- abc3.com/products

3.) Outputting links to nodes:
When I try to link to one of these pages (nodes) via the Drupal's "link fields" the internal url is outputted.
Frontend: It renders the internal url instead of the ones, that were created via the domain path module.

"abc1.com/node/213" instead of "abc1.com/products".

Conclusion:
It seems as if the link field does look for an alias which does not exist. I would expect that the domain path logic would make it possible for the field_link module to use the before defined "abc1.com/products" url in any way.

I have not tested other output widgets like menu links. Guess the behavior is the same.

agentrickard’s picture

This is great, very clear. I haven't been working on the D8 version, but does the issue go away if you add "/products" in the default Path Alias field?

Drupal probably doesn't do a path lookup if that alias is blank, and it's the path lookup that should trigger loading of the domain-sensitive aliases.

agentrickard’s picture

So I thought about this some more, and I think we're missing the obvious.

If all three domains share the same alias, just enter "/products" in the default Path field. Domain Path should only intervene if the path has to be different on each domain.

The Twig template issue is different. The default methods for building a URL are not domain-sensitive, so you need to add a little extra to your code.

See, for reference:

#3085376: Return domain url rather than canonical url
#3081102: Create a Link including the domain programmatically

See especially the code in https://www.drupal.org/project/domain/issues/3081102#comment-13264914

I don't know how to turn that into a Twig function, but you can run it as a template preprocess.

mogio_hh’s picture

Thanks for your reply.

Unfortunately this will not work.
When adding the "/products" to the default alias field a validation error is thrown.

Domain path "/products" matches the default path alias. You may leave the element blank.

Also Drupal in generell does not allow to use the same alias twice. There must be another way... like using logic on hook_link_alter.
Asking first if the link is a valid internal domain_path "path" and then afterwards getting the correct "domain path" including the domain itself in an absolute manor.

I though the \Drupal::service('domain_path.path_processor'); service could be helpful in any way... But I had no success with that.

agentrickard’s picture

Sorry. I am misunderstanding.

You are dealing with multiple nodes, and you want different nodes to have the /products path on specific domains.

That seems to be working, but you are having difficulty writing the proper links in Twig.

Is that right?

If so, see #3081102: Create a Link including the domain programmatically.

mogio_hh’s picture

Yes. I have problems to output the proper "/products" path in url's instead of the core variant "/node/xx".

That's for both.
Twig field entity value but also the straight field rendering via the content array (no custom twig tpl).

agentrickard’s picture

I haven't been working on this module in D8, so it might be a change in recent versions of core -- especially with revisionable paths -- and how links are generated. This should Just Work.

I just jumped in to clarify the "redirects" issue and thought I might be able to assist, but I cannot.

mogio_hh’s picture

Guess we have to dissallow autoselect for links... so urls will be absolute and static then.
Thanks for looking into the issue.

mogio_hh’s picture

I created some logic, that at least does return the "pretty" variant. I created a twigExtension, that creates a function with this logic. Afterwards I use it in my templates.

if (substr($url->toString(), 0) !== "/" && $url->getRouteName() === "entity.node.canonical" && strpos($url->toString(), '/node/') && substr($url->toString(), 0, 4) === "http"){

              $dp_storage = \Drupal::entityManager()->getStorage('domain_path');
              $domain_path_entity = $dp_storage->loadByProperties(['source' => "/node/" . $url->getRouteParameters()['node']]);
              if(sizeof($domain_path_entity) >= 1){
                $domain_path_entity = reset($domain_path_entity);

                $da_storage = \Drupal::entityManager()->getStorage('domain_alias');

                $domain =  \Drupal::service('domain.negotiator')->getActiveDomain();
                $current_environment = $domain->alias->getEnvironment();
                $domain_alias_entity = $da_storage->loadByProperties(['domain_id' => $domain_path_entity->getDomainId(), 'environment' => $current_environment]);
                if(sizeof($domain_alias_entity) >= 1){
                  $alias = $domain_path_entity->getAlias();
                  $domain_alias_entity = reset($domain_alias_entity);

                  global $base_url;
                  $base_url_parts = parse_url($base_url);
                  $scheme = $base_url_parts['scheme'];
                  $return_url = $scheme . "://" . $domain_alias_entity->getPattern() . $domain_path_entity->getAlias();
                }

              }


            }
agentrickard’s picture

In this last part, getPattern() isn't trustworthy because it supports wildcards (*) in the pattern:

$return_url = $scheme . "://" . $domain_alias_entity->getPattern() . $domain_path_entity->getAlias();

If you are just loading alias matches for the current environment and current domain, those should already be applied to the $domain object.

See domain_alias_domain_load().

In that case, you should be able to do this instead:

$domain =  \Drupal::service('domain.negotiator')->getActiveDomain();
$return_url = $domain->getPath() . $domain_path_entity->getAlias();

You can inspect these values by using the 'Domain server information' block.

If you want the scheme to vary based on the request, set your domain records to 'Variable' scheme.

mably’s picture

Status: Active » Postponed (maintainer needs more info)

Is it still an issue?

mably’s picture

Unless new, valuable information is provided, this issue will be closed in a few weeks.

mably’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Problem is most probably fixed in latest 2.0.0-alpha5 release.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.