Jump to content

Adopt field values as defaults from parents


blynx
 Share

Recommended Posts

Hi,

I just made this module because I needed it.

GitHub: https://github.com/blynx/AdoptDefaultsFromParents

Module Directory: http://modules.processwire.com/modules/adopt-defaults-from-parents/

What it does:

It fills fields of newly added pages with corresponding field-values of their parents. Configurable by setting "giving templates" and "adopting fields".

Configuration:

You have to set templates which are able to give field values and fields which are able to receive values. Then, this module automatically applies field values to every newly added page from their parents according to those settings.

Also you can switch between immediate or closest parent (@LostKobrakai, thanks for the hint!)

Why:

I am building a little webshop with padloper and made some product categories ("parent page") which have some properties which should be automatically descendable to their children. Yet, the children should be able to have individual values if they want to.

Now:

So far it works for me - but please test it out and if you think improvements can be made, tell me about it.

Also, what do you think about the general idea?

Notes:

So far I haven't tested it with more complex fields, yet. So far tested and working:

  • Integer
  • Text
  • Page

Similar fields should work. The module simply copies the field value.

cheers!

UPDATES

v003 - 10.5.2016

  • Added option to switch between immediate or closest parent
  • Like 8
Link to comment
Share on other sites

Thanks for the module!

Another approach to inherit the values of parent page fields:

$foo = $page->foo ?: $page->closest("foo!=''")->foo;

So if field "foo" is empty for the current page $foo gets the value of the closest parent in which field "foo" is not empty. Not to say this is as flexible as your module approach though.

  • Like 2
Link to comment
Share on other sites

Another approach to inherit the values of parent page fields

That's basically the approach I used here: https://processwire.com/talk/topic/10817-field-inheritance/ 

The benefit of it is, that the field can stay empty and will therefore automatically adopt any changes to the parent page's value as long as it doesn't have an own value specified.

  • Like 1
Link to comment
Share on other sites

$foo = $page->foo ?: $page->closest("foo!=''")->foo;

Oh thats nice!

I didn't know about the closest() method, though I was thinking about whether this should traverse the whole parent tree or just stick to the immediate parent.

Maybe this could be an option in the module

  • value from immediate parent
  • closest parent

Does it make sense? Or is the immediate parent pretty much useless then?

@LostKobraKai

Nice!

Haven't thought about this approach - but I think I will stick to my version for this site since it doesn't "automagically" fill values on the frontend which are "actually holes" on the backend.

Link to comment
Share on other sites

$foo = $page->foo ?: $page->closest("foo!=''")->foo;

Just a note, closest() isn't even needed here, because we already know the current page doesn't have a value for foo. ->parents("foo!=''") will do the same excluding the current page from the possible resultset.

It's rather this where closest() shines.

$foo = $page->closest("foo!=''")->foo
  • Like 3
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...