Jump to content
louisstephens

[SOLVED] Passing "parent page" to page creation via api

Recommended Posts

I am wondering, how do you pass a variable into wire('page')->get() inside a function? I have been looking through the forums, but unfortunately I have not found the answer yet.

My current set up is:

function generateNewPages($parentPageName) {
    $p = new Page();
    $p->template = "parent";
    $p->parent = wire('pages')->get('/home/');
    $p->name = $parentPageName;
    $p->title = $parentPageName;
    $p->of(false);
    $p->save();

    $p2 = new Page();
    $p2->template = "child";
    $p2->parent = wire('pages')->get('$parentPageName');
    $p2->name = "child";
    $p2->title = "Child Page";
    $p2->of(false);
    $p2->save();
}

When I try to run it by passing in a title like generateNewPages('Demo');, "Demo" is created, but when it gets to the child page I get:

Unknown Selector operator: '$' -- was your selector value properly escaped?

Is there a way to pass the $parentPageName to "wire('pages')->get('$parentPageName')" ?

Share this post


Link to post
Share on other sites
13 minutes ago, louisstephens said:

Is there a way to pass the $parentPageName to "wire('pages')->get('$parentPageName')" ?

Use double quotes!

  • Like 2

Share this post


Link to post
Share on other sites
1 minute ago, elabx said:

Use double quotes!

Or no quotes at all 🙂

Share this post


Link to post
Share on other sites
Just now, kongondo said:

Or remove them. You don't need them in this case 🙂

Thanks everyone for the quick responses. Unfortunately when I do:

    $p2->parent = wire('pages')->get($parentPageName);

I get the following:

Exception: Unknown Selector operator: '' -- was your selector value properly escaped? (in MAMP/htdocs/development/pw/wire/core/Selectors.php line 378)

Im a little baffled as I thought it would work without the quotes as I dont see any glaring errors in Visual Studio Code.

Share this post


Link to post
Share on other sites

Yeah, go with:

$p2->parent = $p

so that you are referencing the page. Otherwise you are trying to find a page by name which actually doesn't work like that.

  • Like 2

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 marcus
      wireshell 1.0.0 is out    
      See Bea's post
       


      -------- Original post -----------
        Now this one could be a rather long post about only an experimental niche tool, but maybe a helpful one for some, so stay with me   Intention Do you guys know "Artisan" (Laravel) or "Drush" (Drupal)? If not: These are command line companions for said systems, and very useful for running certain (e.g. maintenance, installation) task quickly - without having to use the Admin Interface, first and foremost when dealing with local ProcessWire installations. And since it has a powerful API and an easy way of being bootstrapped into CLIs like this, I think such a tool has a certain potential in the PW universe.    It's totally not the first approach of this kind. But: this one should be easily extendable - and is based on PHP (specifically: the Console component of the Symfony Framework). Every command is tidily wrapped in its own class, dependencies are clearly visible, and so on.   ( Here was the outdated documentation. Please visit wireshell.pw for the current one )
    • By David-M
      anyone, any ideas how to solve? 
      i get following error on a website:
      Notice: Undefined variable: out in /var/sites/i/islandmemorials.co.uk/public_html/site/templates/includes/functions.php on line 43
      and this is a code in that very functions file:
      <?php function numberOpen() { date_default_timezone_set("Europe/London"); $currentHour = date('G'); if($currentHour > 9 && $currentHour < 21) { $openFor = 21 - $currentHour; if($openFor > 1) { echo "We're currently open for another ".$openFor." hours, why not pop in?"; } else { echo "Our lines are open for another hour - <a href=\"tel:+01983 857 600\">01983 857 600</a>"; } } else { echo "Unfortunately, our lines are currently closed, but you can still send a message below"; } } function getSubNav($pid) { $test = wire('pages')->get("/services/"); $parents = wire('page')->parentID; $template = wire('page')->template; $out = ''; //echo "pid".$pid."<BR>"; //echo "parents".$parents."<BR>"; //echo "template".$template."<BR>"; $children = $test->children; foreach($children as $child) { $class = $child->id === $pid ? " active" : 'inactive'; if ($child->id === $pid ) { $class= 'active'; } elseif ($template= "memorials" && $child->id === $parents ) { $class= 'active'; } $out .= "<li><a class='{$class}' href='{$child->url}'>{$child->title}</a></li>"; } return $out; } ?> i suppose this has to do with a variable scope, but not sure
    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • 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 Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...