Jump to content

How to override 404 for unpublished pages


alexcapes
 Share

Recommended Posts

Hi,

I have a custom built module that auto-generates a number of pages (translations) on creation of a master page (English version) and saves them as subpages of their respective languages.

Home

- Articles (English)

-- Article title

- French

-- Articles

--- Article title (unpublished)

- German

-- Articles

--- Article title (unpublished)

etc

The pages that are created are unpublished and I'd like if possible to be able to pull content for the English version until a translated version is published. Sometimes no translated version will be available so the English content is seen as a 'fallback' here.

Currently to a logged out user trying to view an unpublished translation page will see a 404.

Could there be a a way to override this behaviour? I'd only want it to be limited to the translations, so template or parent specific.

Extra info...

  • Every translated version has a pagefield storing the master English version, so that info is available for pulling out into a template or module.
  • Each article and it's translations have the same name but is / a country code, such as /fr/ or /de/ etc

Any help much appreciated!

  • Like 1
Link to comment
Share on other sites

Something like this 

// in modules init()
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'redirectToEnglish');

public function redirectToEnglish($event){
  if($this->page instanceof Page && $this->page->is("template.name^=article_")){
    $this->session->redirect($this->page->pageField->url);
  }
}
  • Like 1
Link to comment
Share on other sites

Something like this 

// in modules init()
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'redirectToEnglish');

public function redirectToEnglish($event){
  if($this->page instanceof Page && $this->page->is("template.name^=article_")){
    $this->session->redirect($page->pageField->url);
  }
}

Rather than a redirect, ideally I'd be able to render the page using the English content. So for example the template file would have checks in whether it was unpublished and if so, grab title, body etc from $page->related_language_page->title, $page->related_language_page->body etc.

A return return $Page->render();  would do I guess unless you want a redirect of cause.

More info: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/Process/ProcessPageView.module#L582

Would you mind explaining this a little further? Ideally not wanting to do a plain redirect to the English page but just be able to render the page and maybe just do conditionals in the template file.

Link to comment
Share on other sites

A return return $aPage->render();  would do I guess unless you want a redirect of cause.

More info: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/Process/ProcessPageView.module#L582

$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'overrideError');	

public function overrideError($event){

	  if($this->page instanceof Page && $this->page->is("template.name*=article")){
			return $this->$page->render();
	  }
	}

This ^ is what I have so far but doesn't seem to be changing anything and is still throwing a 404 unpublished pages.

Link to comment
Share on other sites

Sure. You're still trying to render the same page that lead to the pageNotFound. It a unending loop this way. You need to render something that's not throwing a 404.

return $this->page->yourPageFieldToEnglish->render();
Link to comment
Share on other sites

I'm trying here, but so far no luck. (Returns empty page)

Sorry...

Only LostKobrakai's redirect seems to work. 

Yeah same here too. This is the redirect code that works...

		public function init() {
		$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'overrideError');
	}

public function overrideError($event){
		$page = $event->arguments[0];

		if($page instanceof Page && $page->is("template.name^=article_")){
		$this->session->redirect($page->related_page_language->url);
	}
	}

This is workable I reckon but the ideal solution would be to able to render the contents of the English version within the page.

Going to keep plugging away to see if I can come up with something that works.

.

Link to comment
Share on other sites

	public function init() {
		$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'overrideError');

	}

	public function overrideError($event){
		$page = $event->arguments[0];

		if($page instanceof Page && $page->is("template.name^=article_")){
			return $page->render();
		}
	}

This gives me an error: Fatal error: Exception: Page '...' is not currently viewable.

Which I can see why, it's trying to render a page that unpublished, so it correctly spits out the error.

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

×
×
  • Create New...