Jump to content

Multisite: $page->url method


nabo
 Share

Recommended Posts

Hello

I configured the CMS multilanguage with a multisite idea. I created template "_site" and the name of the template is the url of the site interested.
This installation is used only as API generator. The problem is that $page->url method returns the url with _site name. 

Now I have this situation
/[language]/[_site name]/example/example...

I would like to have this
/[language]/example/example....

Is there a way to rewrite this method? The best solution could be a template type check that exclude the _site name in the result.

This solution should work also when I select a page in CKEditor to set a link.

Thanks 

Link to comment
Share on other sites

Hi @Robin S

I updated my ready.php including this 

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->id === 1 || $page->template=="_site") return '/';
  $path = '';
  $parents = $page->parents();
  foreach($parents as $parent) if($parent->id > 1 || $parent->template!="_site") $path .= "/{$parent->name}";
  $event->return = $path . '/' . $page->name . '/';
});

but nothing different from usual happened... if I tried to log something there's also a Fatal Error

Link to comment
Share on other sites

A couple of issues I can see there.

46 minutes ago, nabo said:

if($page->id === 1 || $page->template=="_site") return '/';

You need to set the $event->return instead of returning a value.

And if you are hooking before and replacing a method with your own code then you need $event->replace = true. See Ryan's example in the topic I linked to.

But in your case a simpler approach could be this (note that this is hooking after because it is modifying the $event->return):

$wire->addHookAfter('Page::path', function(HookEvent $event) {
	$page = $event->object;
	if($page->rootParent->template != '_site') return;
	$event->return = str_replace('/' . $page->rootParent->name, '', $event->return);
});

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...