Jump to content

Getting the Parent of selectable page(s) via the API


adrian
 Share

Recommended Posts

Hi everyone,

I am having trouble figuring out how to get the "Parent of selectable page(s)" so that I can do a foreach on the children. This is for a front end form template that builds the form from the fields in a 'questions' template.

Right now I have something like this which works great for text fields.

$questions = $page->fields
foreach($questions as $question){

        if($question->name != 'title'){
            $field = $modules->get("InputfieldText");
            $field->label = $question->label;
            $field->attr('id+name',$question->name);
         }
}

I am not sure if I need to change 'InputfieldText' to work with a 'Page' field type or not. But the real problem is figuring out how to get the parent of the selectable pages for $question.

I know this is probably very easy.

Thanks

Link to comment
Share on other sites

This should get you pretty close:

...
if($question->name != 'title'){
    $inputfield = $question->getInputfield($page);                                  |
    $parent_id = $inputfield->parent_id;
...
Link to comment
Share on other sites

Thanks teppo - that helps a lot.

This works:

    $inputfield = $question->getInputfield($page);
    $options_parent_id = $inputfield->parent_id;
    $options_parent = $pages->get($options_parent_id);

    $options = $options_parent->children();

    foreach($options as $option){

But I have to admit to not understanding why I can't replace:

$options_parent_id = $inputfield->parent_id;

 with:

$options_parent = $inputfield->parent;

and skip the next line altogether. Why do I need to get the id first and then get the page from that?

Not sure if there is a more efficient way to do this.

Link to comment
Share on other sites

In this case I think it's because the Inputfield keeps a copy of the parent ID rather than an instance of the page. The term 'parent' already has another meaning with Inputfields as one Inputfield can be a parent of another (like a Fieldset being a parent of a Text field, or the like). 

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