Jump to content

Using $page->render() on unpublished pages return 500 Error


Recommended Posts

Posted

Hi,

I am trying to give my editors the option to share unpublished pages with externals for review, without moving or copying the actual page in the admin.

I have now created a draft template, that has one field (a page). Whenever the draft template is accessed, it should render the page selected in that field. This works fine as long as I am logged in. When I am not logged in, the $page->render() on the unpublished pages leads to 500 internal server error.

Any idea why? Below the code I am using. Can I use $page->render() on unpublished pages?

Thanks!

Lars

<?php
	if($page->draft_article) { //draft_article is the field where I select the article to be shown
		$out = $page->draft_article->render(); //when the page selected in draft_article is unpublished, guests get a 500 error with this line
		echo $out;
	} else {
		echo 'No article selected.';
	}
?>
Posted

I never really use render much as I prefer complete control over my markup. However, have you tried

foreach($page->draft article as $draft) {
    echo $draft->render(); 
} 
Posted

if ($page->draft_article) will always return true, no matter if it is published or not.

Remember that $page->draft_article is an object, so you're asking if (object)

Probably you're better of with: $page->draft_article->viewable() or $page->draft_article->editable() or something.

if ($page->draft_article->id && $page->draft_article->viewable()) {

}
I'm nor saying this is a solution, but it it can get you started.
Posted

$page->render checks Page::viewable() for unpublished pages, so you'll likely have to hook into viewable and override the return value for your use case.

  • Like 1
Posted

Thanks! Seems to be the issue that render check if the page is viewable, as when the page is in any other status, the code works fine. Still surprised that this returns a 500 internal server error.

If $page->render checks if the page is viewable, is there any other way to display an unpublished page? Publishing and keeping the page hidden might work, but will involve additional steps of hiding / unhiding the page ...

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
  • Recently Browsing   0 members

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