Is possible to apply a default template to child resource of its parent?

Hi,
I am back with MODX and I am building a new web site with members of a association.

I have a parent resource “Member” and I applied the member template.
As child resources, I created 5 profiles.
Form the Members resource, I use getResources to print the title, longtitle, a TV (portrait) and a text (content) of each child resource (the profile).

I do not really need a template for the profile resource, excepted for the TV. (For now, while I create a profile resource, I apply “empty” as a template.)
I created a TV to upload a portrait picture and I want to apply the TV to the profile resource.

However a TV must be allowed to use a template, isn’t?

My idea, is to create an empty template (with no HTML code) named “profile” and I apply it to each profile resources. In that way, I will be able to edit a profile resource and upload a picture for a member.

If in the future, if I create a profile resource, is there a way to automatically apply the profile template to the new profile resource, without a user action?

Or is there an other way to save a picture in a profile resource without the need of a template?

Thanks

Do you even need a resource for these members? If you don’t need a page (with a URL) for every member, then maybe use MIGX instead.


There exists a system setting automatic_template_assignment. If set to “sibling”, a new resource should get the template from its siblings.
(In MODX 3, this currently only works if the template picker isn’t used → system setting enable_template_picker_in_tree = “No”).


You could also use Collections. If the “Members” resource is a Collections-Container, you can then define the template for newly created child resources.

1 Like

Hello
I looked at your proposition but none fit to my need, sadely.

However, I found this option MODX 3 - Dictate the template when a child is created of a certain parent - #6 by bobray

I tried it but…


… when I save the resource, the saving bar is turning for ever

If I remove it, it save the resource in 1 sec.

That option look nice, because I would be able to set the hidemenu to true, as well

Thoses child resource will only “store” the memeber information, save in tile, longtitle, description.

With getResource, I will retrieve it to be display in the parent resource

I use ForceTemplate for this. It works really well for me.

In MODX3 you might want to disable the Template Picker [enable_template_picker_in_tree] system setting as ForceTemplate does not work with this.

Alternatively, there’s the automatic_template_assignment system setting which, if set to sibling, will pick the most commonly used template under your parent. This will affect your whole tree though - whereas ForceTemplate can be customised for each parent.

The problem with this solution is that the template isn’t forced until the new Resource is saved for the first time - so you won’t see your TV / form customisation until then.

ForceTemplate should do what you want, I think.

Thanks a lot, it’s works nice!!!
Would you enable the option to check if the parent duplication?

A parent has a unique ID, Isn’t?

1 Like

I’ve never had to enable that.

It sounds like it just checks that you haven’t specified more than one template to force for each parent.

Think you can ignore it.

There is a syntax error in Bob’s code. The closing bracket ) is missing:

if ($resource->get('parent') == 12) {
...

If you don’t intend the ‘member’ resources to ever be requested directly, it can be easier to use MIGX for such use cases. Instead of the resource fields “pagetitle”, “longtitle” etc., your define the necessary fields in MIGX and use “getImageList” to display the members (similar to “getResources”).

If you never used MIGX, then maybe take a look at this introduction video.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.