Content field inheritance


is there any way to achieve a content inheritance? Let’s say any of my pages could have a field twitter. If for a page this field is empty or does not exists, I would like to use the parent’s field twitter which is closest and not empty; or, if no parent has a twitter field set, fall back to a default.

Take this content structure, where the twitter field may or may not exist. If it does not exists it should inherit its value from the closest existing parent’s value.

home (no `twitter` field exists. Fallback to -> example)
Project 1 (`twitter` field exists, but is empty. Fallback to -> example)
    | Subpage 1 (`twitter` field does not exists. Inherits from parent -> example)
    | Subpage 2 (`twitter` is set to -> sub12)
Project 2 (`twitter` is set -> project2)
    | Subpage 3 (`twitter` field does not exists. Inherits from parent -> project2)
        | Details 3.1 (`twitter` exists but is empty. Inherits from closest parent where it is set-> project2)
        | Details 3.2 (`twitter` is set -> details32)
    | Subpage 4 (`twitter` is set -> sub4)
        | Details 4.1 (`twitter` exists but is empty. Inherits from parent -> sub4)
        | Details 4.2 (`twitter` does not exist. Inherits from parent -> sub4)
        | Details 4.3 (`twitter` is set -> details43)

I know of the $page->parents() function, with which I could iterate over all parent pages, but maybe there’s an easier out-of-the-box method for this which I have missed. I’d be grateful for any suggestions :slight_smile:

If you need this function more often, it would probably make sense to create a custom page method (e.g. getTwitter). You would then define your iteration function only once and can call the method easily in your templates ($page->getTwitter()).

Ah, that’s a good suggestion! Thanks @texnixe.
I assume that means there’s no built-in way of achieving content inheritance then?

No, not that I am aware of, I’m afraid. The only way I can think of is to set a default value via blueprint, but you can’t do that dynamically unless you create a custom field, I guess. Another option would be to use a hook that checks on each page update if the field was filled in and if not, add the value from the parent page. But maybe that’s a bit too much overkill and the custom page method is the easiest way to achieve that.

Thanks again! Will then do it with the custom page method :thumbsup: