Jump to content

Dynamic Parent of Selectable pages


peterfoeng
 Share

Recommended Posts

Hi everyone,

I am wondering if we are able to change the parent of the selectable pages dynamically? potentially to the parent of the current page that is being edited? 

I am trying to search the forum but nothing comes up potentially because of the search breaks down each word. Thank you.

post-1712-0-34169600-1397101331_thumb.pn

  • Like 1
Link to comment
Share on other sites

I got this mixed up...sorry guys,

What I am asking here is when we use a page fieldtype in our template we are able to create a new page; can we somehow dynamically choose the location of the parent where the new page will be created? Like the diagram below

|- Parent 1 (can we dynamically select the parent of the new page to Parent 1 or Parent 2?)

  |- Child 1

  |- Child 2

  |- Child 3

|- Parent 2 (can we dynamically select the parent of the new page to Parent 1 or Parent 2?)

  |- Child 1

  |- Child 2

  |- Child 3

I hope this clears out the confusion. Many thanks

Link to comment
Share on other sites

If I understand correctly I don't think there is a built in way of doing this, but I have done something like this programmatically in front-end forms, so you could do the same with a hook on an admin form template.

Curious though what your use case is. Couldn't you just have two page fields with the different parents?

If not, one solution might be to hook on ProcessPageEdit::processInput and take the value from a text field (child_new) that would be where you'd enter the name of the new page, and then have a select for choosing which parent you want it added to (whichparent). I would actually disable the allow new pages to be create from field option to avoid confusion. Then after you have created the new page from the value of the text field, set it to blank so it can be used again.

Sorry I don't have much time right now, so this should be considered pseudo code and ugly pseudo code at that, but maybe it will get you going!

class NewPageCreator extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'New page creator',
            'summary' => '',
            'href' => '',
            'version' => 1,
            'permanent' => false,
            'autoload' => true
        );
    }

    public function init() {
        $this->addHookAfter('ProcessPageEdit::processInput', $this, 'addToParent');
    }

    public function addToParent($event){

        // ProcessPageEdit's processInput function may go recursive, so we want to skip
        // the instances where it does that by checking the second argument named "level"
        $level = $event->arguments(1);
        if($level > 0) return;

        $pp = $this->post->input->whichparent;

        if($this->input->post->child_new == '') return; //if no child_new entered, leave now

        $newchild = new Page();
        $newchild->template = $this->templates->get("template-name");
        $newchild->parent = $pp;

        $newchild->of(false);
        $newchild->title = $this->sanitizer->text($this->input->post->child_new);

        $newchild->save(); // Save the new child

        $pp->test_page = $newchild; //add it to the list of selected pages for the page field
        $pp->child_new = ''; //empty the child_new field - it is now in the main child field
        $pp->of(false);
        $pp->save("child_new");

    }

}

I revised this significantly and did a little testing. This should now pretty much work, although note that field names for child_new and whichparent are hardcoded.

Edited by adrian
  • Like 5
Link to comment
Share on other sites

  • 3 months later...

So it's not possible to use "custom php to find selectable pages" and still provide the ability to create new pages on the fly right?

What about the possibility to enter options like

selectablePages(array(

   parent => '',

   template => '',

);

and so on so it's possible to have a dynamic parent/template and still be able to create a page through the field

or like just let the editor choose the template like when using the quickmenu for creating pages on the /processwire/page/ PageList for a template which allows multiple templates for children

Or am I missing some other magic for doing this? :)

Link to comment
Share on other sites

  • 3 years later...
On 7/24/2014 at 5:13 AM, Can said:

So it's not possible to use "custom php to find selectable pages" and still provide the ability to create new pages on the fly right?

What about the possibility to enter options like

selectablePages(array(

   parent => '',

   template => '',

);

and so on so it's possible to have a dynamic parent/template and still be able to create a page through the field

or like just let the editor choose the template like when using the quickmenu for creating pages on the /processwire/page/ PageList for a template which allows multiple templates for children

Or am I missing some other magic for doing this? :)

Justin case someone looks for an answer, it is possible to dynamically set the selectable pages (instructions are in current PW page field options). And to dynamically change the parent of the "to be created pages" from the Create New textarea, it is possible to hook into InputfieldPage's method processInputAddPages, set "template_id" and "parent_id" configuration data on the fly with the information we have available such as the $page where the field instance is being called. If you take notice, later on the hooked method, this two options are read back to accomplish the page creation.

$wire->addHookBefore('InputfieldPage::processInputAddPages', function($event) {
    $field = $event->object;
    $page = $field->hasPage;
    
    //Set the parent and template options, this are checked later to complete the task of adding pages
    $option_template = wire("templates")->get("template_name")->id;
    $parent_id = wire('pages')->get("name=options")->id;

    if($page->template == "some_template"){
        //Example, change the parent depending on what template the field's currently in.
        $parent_id = $page->child('template=custom_options')->id;
    }
        
    $field->set("template_id", $option_template);
    $field->set("parent_id", $parent_id);
  
});

 

  • Like 4
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...