Jump to content

Inputfield Dependencies parent_id


a-ok
 Share

Recommended Posts

Do you need this for a frontend form or in the admin?

If you are building a frontend form via the API, you could try something like

//get page for which to build the form
$editpage = $pages->get("template=mytemplate, other selectors"); // just example selectors, they depend on your needs
$parentID = $editpage->parentID;

// create a new form field (also field wrapper)
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'myform');

// create field only if page parent_id=1032
if ($parentID == 1032) {
    $field = $modules->get("InputfieldText");
    $field->label = "Label";
    $field->attr("id+name","myfieldname");
    $field->attr("value",$editpage->myfieldname);
    $form->append($field);
}
//...other fields

If you could explain your use case in more detail, I'm sure we can find a solution that fits your needs.

Link to comment
Share on other sites

Hello Richard,

Field Depedencies work with Inputfields - parent_id is not an Inputfield for the form. It is a property of the Page-object (which you cannot use).

The selector string just looks like a normal selector, but it is not a normal selector. The same syntax is just used here because it feels familiar.

  • Like 5
Link to comment
Share on other sites

Thanks both for your help.

I guess since I need to use this in the admin, not as a front-end form, then I cannot show a specific field based on the parent_id/parent? Would be nice to expand this functionality out in the future, as I guess it could become quite useful.

Here is my setup... I have a 'News page', with two child pages 'Jobs' and 'Funding'. These child pages act as categories/filters so users can view all news, just jobs, or just funding. Each page within these categories uses the same template, and has a field 'closing_date' and 'attachments' so if the page you are adding has 'Job' as a parent, then it should show these fields... otherwise not. So maybe I just create two templates... but just thought I would try to make it as global as possible.

Thanks again

R

Link to comment
Share on other sites

One of the key design concepts of ProcessWire is that you can use the same fields in multiple templates. Behind the scenes, the values of a field are always stored in the same table (regardless of the template where it is used). This makes it possible to query based on a field without even having to know in which templates it is used. In this respect, it would be completely ok to have multiple templates.

Anyway, to answer your question. You cannot currently create a rule that's based on the parent using the default "Show if" -functionality.

Now if you want to use the same template, have you considered making the category a Page-field? You could arrange your tree like this

News

  * News item 1

     (Category = Jobs)

  * News item 2

     (Category = Jobs)

  * News item 3

     (Category = Funding)

Categories

  * Jobs

  * Funding

The Category-field of the News-template would be a Page-field, that has it's selectable parents set to "Categories". On the frontend you could easily provide filters by listing the pages under "Categories".

Implementing the categories this way you can use the built-in "Show if" -feature.

  • Like 3
Link to comment
Share on other sites

Hi @sforsman and thanks very much for this answer. This makes perfect sense and is actually how I structured this before, and the 'Show if' worked. However, I needed to add filters for this, so you could view only Jobs, or only Funding news articles. /news/jobs/ or /news/funding which is why I ended up making each news post a child of those categories. If there's a way to have this /news/jobs/ with them acting as 'categories' then greta but I didn't think this would be possible either.

Nonetheless, thanks so much for your explanation and help.

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

×
×
  • Create New...