Jump to content

Changing parent of a page via API


JoshoB
 Share

Recommended Posts

So I've been trying to do something seemingly simple. I performed a search here and I keep getting similar results, so I'm sort of lost as to what I'm doing wrong.

The issue is as follows: I'm building a website where people can train their vocabulary for different languages. It's based on books we've published. At first, I imported all the data as just one big pile of words, with the exercises and sentence numbers added to the file. But now, it's more useful to organize the pages differently, with separate words having a new template as their parent based on the exercise.

Let me illustrate for a moment.

The present situation looks like this:

Home (home)
|-Language
|  |-English (words)
|     |-Word #1 (word)
|     |-Word #2 (word)

What I'm trying to do is create the following:

Home (home)
|-Language
|  |-English (words)
|     |-Exercise #1 (exercise)
|     |  |-Word #1 (word)
|     |  |-Word #2 (word)
|     |-Exercise #2 (exercise)
|     |  |-Word #3 (word)
|     |  |-Word #4 (word)

Fortunately, every word has a field "exercise" that lists the number of the exercise in question. So it shouldn't be too hard to insert the proper parent page here. Or so I thought, at least.

Since this is a one-time operation, I wrote some quick-and-dirty code in the "words" template. It looks like this:

// Collect the words
$words = $page->children->find("template=word,exercise>0,exercise<=1"); 

// Cycle through words
foreach($words as $word) {

	// Does an exercise exist with this number?
	$number = trim($word->exercise);
	if($number < 10) $number = "00$number";
	elseif($number < 100) $number = "0$number"; 

	// If the page doesn't exist, create it
	$exercise = $page->children->get("template=exercise, name=exercise-$number");
	if(!$exercise->id) {
		$p = new Page(); 
		$p->template = 'exercise'; 
		$p->parent = $page; 
		$p->name = "exercise-" . $number; 
		$p->title = "Oefening " . trim($word->exercise); 
		$p->save();
	}

	// Should this page be moved?
	if($word->parent->template != "exercise") {

		// Disable output formatting
		$p = $word; 
		$p->of(false);
		$parent = wire('pages')->get("parent=$page, template=exercise, name=exercise-$number"); 
		$p->parent = $parent; 

		// Save
		$p->save;

		// Re-enable output formatting
		$p->of(true); 		
	}
}

Now, I've rewritten this countless times. The script does create the new pages with the "exercise" template, right where they should be, but it doesn't change the parent of the pages with template "word" to these new pages. I honestly don't see what I'm doing wrong, but undoubtedly someone here will glance at it and go, "Hey, you oughta do this!" and I'll slap my forehead and wonder why I didn't think of it.

Anyway, thanks in advance!

 

 

 

Link to comment
Share on other sites

Don't do this part...

$p = $word;

...just work with $word directly to change its parent.

If it still not working in all cases then focus on the step where you get the parent page:

$parent = wire('pages')->get("parent=$page, template=exercise, name=exercise-$number");

There is no guarantee this will always return a valid page, so you might want to check that $parent has an id > 0 and if not log/dump $word->id or something so you can find where the problem is.

  • Like 1
Link to comment
Share on other sites

6 minutes ago, Robin S said:

Don't do this part...

Yeah, I know. Like I said, I rewrote this countless times and in the end I thought "Maybe it helps if I store the $word as a separate variable?"

Anyway, I made some changes. The modified part now looks like this:

// Should this page be moved?
if($word->parent->template != "exercise") {

	// Disable output formatting
	$word->of(false);
	$parent = wire('pages')->get("parent=$page, template=exercise, name=exercise-$number"); 
	if($parent->id) {
		$word->parent = $parent; 
	} 
	else {
		die("Error: parent doesn't exist?"); 
	}
		
	// Save
	$word->save;

	// Re-enable output formatting
	$word->of(true); 
}

And it does nothing: it just runs the script and doesn't give an error, doesn't move the pages, etc.

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