Jump to content

cloning a page with pagetable field via api


bernhard
 Share

Recommended Posts

i have problems cloning pages with pagetable field via api... i get the following error: 

Error: Exception: Can't save field from page 1039: /original-page-1/: Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved. [Page::statusCorrupted] fields: pagetable (in /var/www/html/wire/core/Pages.php line 1272)

#0 [internal function]: Pages->___saveField(Object(Page), 'pagetable', Array)
#1 /var/www/html/wire/core/Wire.php(366): call_user_func_array(Array, Array)
#2 /var/www/html/wire/core/Wire.php(321): Wire->runHooks('saveField', Array)
#3 /var/www/html/wire/core/Page.php(1272): Wire->__call('saveField', Array)
#4 /var/www/html/wire/core/Page.php(1272): Pages->saveField(Object(Page), 'pagetable', Array)
#5 /var/www/html/wire/modules/Fieldtype/FieldtypePageTable.module(216): Page->save('pagetable')
#6 /var/www/html/wire/core/Wire.php(388): FieldtypePageTable->hookPagesCloned(Object(HookEvent))
#7 /var/www/html/wire/core/Wire.php(321): Wire->runHooks('cloned', Array)
#8 /var/www/html/wire/core/Pages.php(1627): Wire->__call('cloned', Array)
#9 /var/www/html/wire/c
This error message was shown because you are logged in as a Superuser. Error has been logged.

code used on a test-setup in "home" template (so $page = /home):

$np = $pages->clone($pages->get('/original-page'), $page, false);
$np->of(false);
$np->title = 'New Page';
$np->name = 'newpage';
$np->save(); 

without the pagetable field everything works fine. using the ProcessPageClone module and clicking on "copy" also works fine!

any help would be very appreciated :)

I'm using dev 2.5.22 - though i don't think it is related to the pw version

Link to comment
Share on other sites

thank you macrura, but that's not the problem:

i have this simple code

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<title><?php echo $page->title; ?></title>
		<link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/main.css" />
	</head>
	<body>
		<h1><?php echo $page->title; ?></h1>
		<?php if($page->editable()) echo "<p><a href='$page->editURL'>Edit</a></p>"; ?>

		<?php
		$np = $pages->clone($pages->get('/original/'), $page, false);
		$np->of(false);
		$uid = uniqid();
		$np->title = $uid;
		$np->name = $uid;
		$np->save(); 
		?>
	</body>
</html>

and this is the problem (please see the 1:48min screencast)

http://screencast-o-matic.com/watch/coeYbres8Q

Link to comment
Share on other sites

In backend it works cause there's no outputformatting. Doing in a template you see the error cause front end template context has output formatting enabled.

Not sure if pagetable causes issues with clone but can't remember if Ryan said something.

So the solution is right there explained in the error if you read it!

Try a $pages->setOutputFormatting(false) before your code.

  • Like 3
Link to comment
Share on other sites

So the solution is right there explained in the error if you read it!

of course i read the error message and of course i did investigate some time to find the problem myself... but "$page->setOutputFormatting(false)" as written in the error was not the solution - and i thought i've already done that by calling $np->of(false). fortunately your suggested $pages->setOutputFormatting(false) did the trick!

the working code is this

$pages->setOutputFormatting(false);
$np = $pages->clone($pages->get('/original/'), $pages->get('/original/'), false); // creates a copy of "original" as a subpage of itself
$uid = uniqid();
$np->title = $uid;
$np->name = $uid;
$np->save();
$pages->setOutputFormatting(true);

$pages->of(false) did NOT work btw! and i didn't find $pages->setOutputFormatting() anywhere in the docs so i thought it were only for single-page instances ($page).

anyway - thanks for the solution! :)

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...