Skip to content

feat(resource): Add childTemplate property for default child template#16898

Open
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Ibochkarev:feat/13753-child-template-property
Open

feat(resource): Add childTemplate property for default child template#16898
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Ibochkarev:feat/13753-child-template-property

Conversation

@Ibochkarev
Copy link
Collaborator

What does it do?

Adds support for the childTemplate template property. When creating a new child resource under a parent, if the parent's template has a childTemplate property set to a template ID, that template is preselected as the default for the new resource (both in the create form and when saving).

Backend (Resource/Create processor): resolveDefaultTemplate() and getChildTemplateIdFromParent() read the parent template's childTemplate property and use it when no template is explicitly provided.

Manager (resource/create controller): getChildTemplateIdFromParentTemplate() sets the default template in the create form. Takes precedence over automatic_template_assignment when childTemplate is set.

Why is it needed?

Allows template authors to define which template should be used for child resources (e.g. News overview → News detail), reducing manual template selection when creating documents in a hierarchy.

How to test

  1. Add a childTemplate property to a template (value = template ID).
  2. Create a child resource under a resource that uses that template.
  3. Confirm the child template is preselected in the form and saved correctly.

Related issue(s)/PR(s)

Resolves #13753

@Ibochkarev Ibochkarev force-pushed the feat/13753-child-template-property branch from b7b3bf5 to 756ba00 Compare February 25, 2026 03:31
@Ibochkarev Ibochkarev marked this pull request as ready for review February 25, 2026 03:32
…modxcms#13753)

- Add resolveDefaultTemplate() and getChildTemplateIdFromParent() in Create processor
- Add getChildTemplateIdFromParentTemplate() in resource create controller
- When creating a child resource, use parent template's childTemplate property if set

Resolves modxcms#13753
@Ibochkarev Ibochkarev force-pushed the feat/13753-child-template-property branch from 756ba00 to b9bbc7f Compare February 25, 2026 03:34
@Ibochkarev Ibochkarev changed the title feat(resource): Add childTemplate property for default child template (#13753) feat(resource): Add childTemplate property for default child template Feb 25, 2026
@biz87
Copy link

biz87 commented Feb 25, 2026

Code Review

Summary

Adds childTemplate template property support — when creating a child resource, the template defined in the parent template's childTemplate property is preselected. Changes in Resource/Create.php processor and resource/create.class.php controller, +143/-26.

Issues

The getChildTemplateIdFromParent() logic is nearly duplicated across two files — the processor (Create.php:330-383) and the controller (create.class.php:313-355). Differences are minor: return type (?int vs int), parent property name, and existence check. This is a DRY violation — a bug fix or behavior change would need updating in both places. Consider extracting the logic into modTemplate as a method (e.g. getChildTemplateId(modResource $parent)).

getObject is used solely to check template existence (Create.php:374, create.class.php:347). A full object load for an existence check is wasteful — $this->modx->getCount(modTemplate::class, $templateId) > 0 would suffice.

Assessment

Useful feature, well-structured with early returns. But the code duplication is a significant maintainability concern.

Verdict

Request changes — extract shared logic to eliminate duplication.

- Introduced `getChildTemplateId()` method in `modTemplate` to encapsulate logic for retrieving the child template ID from a parent resource.
- Updated `Create` processor and resource create controller to utilize the new method, simplifying the code by removing redundant methods.
- Ensured that the child template ID is correctly resolved when creating child resources.

This change enhances code maintainability and readability by consolidating the child template retrieval logic into a single method.
@Ibochkarev Ibochkarev added the feature Request about implementing a brand new function or possibility. label Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Request about implementing a brand new function or possibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Set childtemplate automatically based on template property

2 participants