MODX 3 - Dictate the template when a child is created of a certain parent

Hey all …

I’ve always used ForceTemplate to dictate the template used for new children of a certain parent.

For example, if I have a parent called “News”, I might force any newly created children of that to default to use a template called “News-Item”.

This worked well in MODX 2.x regardless of whether the child resource was created or quick-created.

In MODX 3 - ForceTemplate no longer seems to have any effect with Quick Create and since that’s now the default way of creating resources when the + sign is used - it’s difficult to enforce this template usage in any way at all as far as I can see.

It does work if the parent is a Collection. Creating a child within the Collection parent bypasses the quick create and uses the correct template as defined in ForceTemplate.

But I’d like to find a solution that covers all bases if possible.

Does anyone else have methods for achieving this?

Thanks in advance.

Just realised that’s not actually true - the Create Document process has a different form from the Quick Create Document one.

I guess the introduction of this intermediate form [with the template preview] for creating documents in MODX 3.x is where the disconnect with ForceTemplate has come from.

Regardless … the search for a solution remains …

The system setting “automatic_template_assignment” might be a partial solution for this.
If it’s set to sibling creating a new resource will use the most used sibling template.

So for children of “News” to use the “News-item” template you only need to create one child and manually pick the “News-item” template (therefore a partial solution).

1 Like

Hey @joshualuckers

Thanks for the reply.

This sounds like it could potentially be helpful - however changing that setting to sibling doesn’t seem to be having any effect on the template used for children of a parent - every new page under the parent still uses the template specified in default_template

I do have children under the parent with the correct template and nothing else. As far as I can see it should work as advertised but it’s definitely still using the default template.

Even if I set the default_template to 0 - new children just default to the (empty) template.

Can someone confirm that the automatic_template_assignment definitely works in 3.x?

Thanks again.

I think the solution is to set the system setting enable_template_picker_in_tree to No.

I believe with this new “Create Document” window in MODX 3, the automatic_template_assignment setting is ignored.

1 Like

If you truly want to “force” the template, you could do it with a plugin attached to OnDocFormSave. Something like this (where 12 is the ID of the News resource and 6 is the ID of its template):

if ($resource->get('parent') == 12 {
    $resource->set('template', 6);

This would ignore any template the user has selected and force the one you want.

You could duplicate the above code with new ID numbers below the code above if you want to force the template for other pages.

1 Like

@halftrainedharry :sparkler::+1:

That’s what I needed, thanks once again.

Setting enable_template_picker_in_tree to No means that the intermediate modal form is skipped and the user is taken straight into the main Create Document page which now defaults to the correct template.

Thanks also @bobray - I was beginning to think about writing a plugin and this would have been a good starting point. The main issue with using OnDocFormSave is that the user wouldn’t see the TVs relevant to the desired template until they’d saved the resource. The main driver behind this is so that users are instantly shown the correct fields for the type of resource they are creating.

Using Harry’s fix has got everything working as it always has before.

Thanks again both.

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