Jump to content
daiquiri

Adding a new home page and moving the current home tree below it

Recommended Posts

Hey, 

This is probably a very basic question but I just spent the past hour messing around trying to get this done and still stuck so hopefully someone can point me in the right direction.

I have a site with a home page containing a ton of content; and a huge hierarchy of pages below.

What I need to do now is create an entirely new home page, with a new template, and make the current home page its child, keeping its content intact - moving it down with the entire subtree

So basically I now have 

  • Old Home Page with old template
    • Some subpage
    • Some other subpage
    • Hundreds of other sub and sub sub sub pages with lots of content, which I don't want to manually move around/clone etc

And what I want to achieve is

  • New home page with new template
    • Old Home Page with old template
      • Some subpage
      • Some other subpage
      • Hundreds of other sub and sub sub sub pages with lots of content, I don't want to manually move around/clone etc

Eg. in WordPress I'd just create a New Home Page, set it to be the Home in theme settings, then make Old Home page it's child which would also make its subpages the grandchildren - very quick and easy.

How do I achieve this in Processwire? I couldn't find a way to add a new page on the same level as the current home, so I could just switch betwen them...

Thanks!

 

 

 

 

Share this post


Link to post
Share on other sites

You can't, without running into major headaches, change the root page (i.e. "Home"). What you can do is create a copy and move all children with the exception of the system fields (admin, trash, 404) under the new home. Then you can adapt the Home page to your liking (i.e. switch template, edit contents, whatever).

Here's a small script that automates that task. This hasn't been tested extensively, so I don't advise to run this on a production system, only on a copy (or at the very least make a test run on a copy and then make sure you have a full, working database backup at hand).

You can copy this code into a script (e.g. as "clonehome.php") in your PW installation root and run it from the command line.

<?php namespace ProcessWire;

if(PHP_SAPI != 'cli') exit;

include('index.php');

error_reporting(E_ALL);
ini_set("display_errors", true);

/* **************************************************************
 * CONFIGURATION FOR CLONING
 * *************************************************************/

$adminUser = "admin"; // PW Backend User

$childrenToLeave = [
	$config->adminRootPageID,
	$config->trashPageID,
	$config->http404PageID = 27
];

/* **************************************************************
 * END OF CONFIGURATION
 * *************************************************************/

$session->forceLogin($users->get($adminUser));

$home = $pages->get($config->rootPageID);

echo "Cloning Home" . PHP_EOL;

$newOldHome = clone $home;
$newOldHome->setQuietly('_cloning', $home);
$newOldHome->addStatus(Page::statusSystemOverride);
$newOldHome->removeStatus(Page::statusSystem);
$newOldHome->removeStatus(Page::statusSystemID);
$newOldHome->id = 0;
$newOldHome->setIsNew(true);
$newOldHome->parent_id = $home->id;
$newOldHome->of(false);
$newOldHome->set('numChildren', 0);
$newOldHome->removeStatus(Page::statusSystemOverride);
$pages->save($newOldHome, ["quiet" => true]);

echo "Home page cloned, new id is {$newOldHome->id}." . PHP_EOL;

$childrenToLeave[] = $newOldHome->id;

echo "Moving children:" . PHP_EOL;

// Now move all children, with the exclusion of admin tree and special pages (404, Trash)
// as configured in $childrenToLeave

foreach($pages->find("parent=$home, include=all") as $child) {
	if(in_array($child->id, $childrenToLeave)) continue;
	echo "- {$child->name}...";
	$child->of(true);
	$child->parent = $newOldHome;
	$child->save();
	$child->of(true);
	echo "[X] Done" . PHP_EOL;
}

echo "FINISHED. All children moved." . PHP_EOL;

 

  • Like 5
  • Thanks 1

Share this post


Link to post
Share on other sites

Thank you very much, Ill try this approach if there's no other way.. But wow, it's quite crazy that there's no simple solution for such a very simple task..

@ryan do I really need to run command line scripts which may or may not mess up my entire site, just to change the home page??

Share this post


Link to post
Share on other sites

@daiquiri - with respect, i think you're missing a larger point about this system.

Basically what you are trying to do doesn't make any sense, and you can't compare to WordPress because WP is a bucket structured system whereas PW is a hierarchical tree system.

The homepage represents the root of your site (/). That is basically a sacred thing here - it should not be changed, and it should not need to be changed so i think you should re-assess why you need/want to do this and come up with a different way or reconfigure your thinking about how to achieve what you want.

and in all of the years of working and developing in this, i have never heard of anyone needing or wanting to clone the homepage. The easy way to do it is:

  • Current Home Page with old template (home.php)
    • New Homepage (subhome.php) move all public content branches below this new branch
      • Some subpage
      • Some other subpage
      • Hundreds of other sub and sub sub sub pages with lots of content, which I don't want to manually move around/clone etc

then just reconfigure your code in home.php and subhome.php (e.g. create a new home.php and rename home.php to subhome.php)

  • Like 4

Share this post


Link to post
Share on other sites

@Macrura with respect, 

Quote

The homepage represents the root of your site (/). That is basically a sacred thing here - it should not be changed, and it should not need to be changed

Unfortunately there's this unconvenient thing called reality..

What you are suggesting is something that would take hours in the current setup I have, setting up all fields/contents again to recreate the current home page in the New  homepage, and spending another crazy amount of time reassigning it as parent of new sites.. Even though it's as simple as changing the site tree in the cms, very easy in almost all cms-es.. And the best argument for this painstaking process being necessary is that the home page is "sacred".. 🙂 I believe sacred things belong in the church.

Share this post


Link to post
Share on other sites

Hi @daiquiri , welcome to PW and the forum!

You got 2 answers of long-time members and even 1 script that you can use within 3 hours. There's no need to take anything personal 😉 

1 hour ago, daiquiri said:

Unfortunately there's this unconvenient thing called reality..

1 hour ago, daiquiri said:

But wow, it's quite crazy that there's no simple solution for such a very simple task..

Reality is something in our heads 😉 The reality is that the changes you are asking for are not a simple task for PW. PW works totally different than other cms's - so it's quite logical that what might be a simple task in other systems can be quite hard in PW, and vice versa. Many of us here love PW for many things, but there are also drawbacks and things that are not so easy, and it looks like you just found one of these things 😉

But often when working with PW and especially coming from other systems there are other ways that you haven't thought of before. That's because your reality is based on the experiences you got from other systems. And that's what @Macrura said with this statement:

1 hour ago, Macrura said:

The homepage represents the root of your site (/). That is basically a sacred thing here - it should not be changed, and it should not need to be changed so i think you should re-assess why you need/want to do this and come up with a different way or reconfigure your thinking about how to achieve what you want.

Call the root page of a PW instance sacred or call it something else - there might be an easier way to achieve what you want in the end...

Share this post


Link to post
Share on other sites

@daiquiri, the homepage is sacred in the sense that it represents the root of the site (/) and all pages must descend from this root page, this is fundamental to the structure of the internet, domains and such. PW was deliberately designed this way from the inception, as it mirrors the "reality" of the internet - the descendant tree structure of a website's pages off the root.

Your content configuration may not be sustainable if you find yourself having to go through the effort you are describing.

But if you know the API, you can easily change pages of any type of template to a new parent with 1-2 lines of api code.

  • Like 1

Share this post


Link to post
Share on other sites
56 minutes ago, bernhard said:

Hi @daiquiri , welcome to PW and the forum!

You got 2 answers of long-time members and even 1 script that you can use within 3 hours. There's no need to take anything personal 😉 

I fully appreciate the responses, and I'm very thankful for the script 🙂 But when I get a response like "what you're trying to do doesn't make sense" and that "you shouldn't ever have to change the home page because it's sacred" - when I'm requested by a designer and a client to do exactly that - add an entirely new home page, and use the current one as a child of that, moving the entire tree down the hierarcy - then all I can say is principles are nice but reality is not always inline with an ivory-tower approach to things, unfortunately.

Share this post


Link to post
Share on other sites
10 hours ago, daiquiri said:

But when I get a response like "what you're trying to do doesn't make sense" and that "you shouldn't ever have to change the home page because it's sacred" - when I'm requested by a designer and a client to do exactly that - add an entirely new home page, and use the current one as a child of that, moving the entire tree down the hierarcy - then all I can say is principles are nice but reality is not always inline with an ivory-tower approach to things, unfortunately.

Heya! Just wanted to drop a quick note here regarding this point 😊

I get why you feel that this is a problem and "an ivory-tower approach to things", but it's worth keeping in mind that with all design decision you'll get some benefits, but also some drawbacks. In systems where there's no "true" tree structure (some CMS products have gone this route) this would likely be an easy thing, but since ProcessWire is indeed hierarchical, it's going to need a bit more work. At the same time the structure in ProcessWire is predictable, efficient, and works great when a site consists of a variety of different content types.

I've worked with other types of systems as well (including WP), and would choose PW's hierarchy any day over any of the alternatives.

Regarding "switching the root", I've run into something similar once, and that's after being around PW for almost a decade and having built, maintained, and rebuilt quite a few ProcessWire sites in that time. What you're asking for may seem like a simple thing, but it's really not (given our context), and it's also quite a rare request — if it wasn't, it'd probably make sense to give it more consideration at core level 🙂

To give you a bit of context, this is roughly the same as a Linux user stating that "I just need to switch the system root, it's an easy thing right?". Well, you can do that as well, but it's really not an easy thing, and may in fact be very destructive (unless you know exactly what you're doing). Again, design decisions; you may disagree with them, but the fact that this issue rarely comes up tells me that this particular design decision was likely a good one.

Just my five cents 🤷‍♂️

  • Like 7
  • Thanks 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...