Jump to content
Harmen

Get child page ID

Recommended Posts

Hello,

 

On my website I want to import categories from another website based on prestashop. For each categorie a new child page will be created, but if I want to import the categories again because there were some changes made in the categories in Prestashop, then the page has to be updated and not create a new page. So I tried the following: 

$parent = $this->session->Parent;
foreach($categories as $item) {
			//make sure end of string ends with alphanumeric
			$str_catlink = $item['link_rewrite'];
			$category_name = preg_replace('/[^a-z0-9]+\Z/i', '', $str_catlink);

			// see if we already have this item
			$page = $parent->child("name=($category_name)");

			// if we don't have this item already then create it
			if(!$page->id) {
				$page = new Page();
	//etc etc etc

But if I want to import the same categories now, there is an error: Call to a member function child() on a non-object. How can this be solved? Does anyone have an idea?

Share this post


Link to post
Share on other sites

You can't store Objects in session.

Share this post


Link to post
Share on other sites
14 minutes ago, Soma said:

You can't store Objects in session.

So, if I declare $parent in another way it will work? I hope so, but without this check if the page already exists, it works perfectly. So I think it is not the session that makes the problem...

 

I wrote this in a module file by the way, maybe that makes sense?

Edited by Harmen

Share this post


Link to post
Share on other sites

Maybe save the parent ID in the session instead and then get the page again using the id. 

Share this post


Link to post
Share on other sites

Why not just do 

foreach($categories as $item) {
    //get the page if it exists
    $p = $pages->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");
    if($p->id){
        // page exists
        $p->title = "new title";
        $p->save();
    } else {
        // page does not exist
        $np = new Page();
        $np->template = "template-of-page-to-be-created";
        $np->parent = $pages->get("/parent-page/");
        $np->title = "the title";
        $np->save();
}

 

  • Like 1

Share this post


Link to post
Share on other sites

 

fbg13, I think your code works but I have to declare $pages. So $pages = ????

My code now looks like this:

protected function importPage() {
		
		$categories = $this->get_category();
		//The CSV means nothing, I modified the importCSVfiles module...
		$parent = $this->session->csvParent;
		
		$Template = $this->session->csvTemplate;

		foreach($categories as $item) {
			$str_catlink = $item['link_rewrite'];
			$category_name = preg_replace('/[^a-z0-9]+\Z/i', '', $str_catlink);

			//get the page if it exists
			$p = $pages->get("parent=$parent,title=$category_name");
			if ($p->id) {
				// page exists
				$p->title = "$category_name";

				$p->save();
			} else {
				// page does not exist
				$np = new Page();
				$np->template = $Template;
				$np->parent = $pages->get("$parent");
				$np->title = "$category_name";
				$np->save();
			}

		}

So, if I can declare the $pages I can try it. Suggestions are welcome...

Share this post


Link to post
Share on other sites

Replace $pages with  $this->pages or wire("pages")

$p = $this->pages->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");

$p = wire("pages")->get("parent=/parent-page/,title=the_title_of the_page_to_be_created_updated");

 

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.

  • Similar Content

    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
×
×
  • Create New...