MODxMy original title for this post was “Use MODx Revo Form Customization to Control Template Inheritance to Grandchildren”, but I decided that was a little too long and geeky. So I shortened it and shed the implied geekiness, eh?

I have been developing a site in which I was needing to control which template a child inherited and which template the subsequent grandchildren of a given document inherited. I realize that there may be more direct methods for accomplishing this (please share them if there are), but I haven’t yet come across them.

Form Customization: Child

I am going to outline how to control template inheritance of a child document.

First, under Security->Form Customization select Create New Profile. Give it a Name, Description and Activate it. Once created, right click the Profile and select Edit.

To create the 1st set, select Create New Set and fill out the following fields accordingly:

  • Action – select Create Resource. This is just the timing for this set, it will run when a new resource is created.
  • Description – fill in your description
  • Template – in this example you can leave this field blank. At this level we will use the Constraint Field to determine which document this is applied to.
  • Constraint Field – You can use any resource field here. In this example we are using ‘parent’.
  • Constraint – fill in the document ID which will house the children and grandchildren. Let’s say it has an ID of 2.
  • Click Save
  • Right click your new set and select Edit
  • Scroll down. Under modx-resource-settings find template and click in the DEFAULT VALUE column. You can now enter text into this field. Enter the Template ID of the template you want the document to inherit. Let’s say it has an ID of 3.
  • Hit Enter and click Save

So, we have built a Set that, at the point of document creation, will asses the Parent field and if it has an ID of 2 it will reassign it’s template field to an ID of 3. Without this Set, the document would have instead inherited the Template ID of it’s parent.

Form Customization: Grandchild

For this step to work I am making a couple of assumptions:

  • That this process is only occurring within the document tree below the document with an ID of 2. This is the ‘Ultimate Parent’ in this example.
  • That these children are the only documents that will be using the template with an ID of 3.

Now, to create the 2nd set, again select Create New Set and fill out the following fields accordingly:

  • Action – select Create Resource.
  • Description – fill in your description
  • Template – select the template with an ID of 3 from above.
  • Constraint Field – in this example you can leave this field blank.
  • Constraint – in this example you can leave this field blank.
  • Click Save
  • Right click your new set and select Edit
  • Scroll down. Under modx-resource-settings find template and click in the DEFAULT VALUE column. You can now enter text into this field. Enter the Template ID of the template you want the document to inherit. Let’s say it has an ID of 4.
  • Hit Enter and click Save

Now, to summarize the entire process: any children of Document ID 2, at the point of creation will be assigned a Template ID of 3 (1st set). Since this template is not being used elsewhere, it is exclusive to this portion of the document tree. With that being the case, using the 2nd Set of the Profile, any grandchild document created will, at the point of document creation, have it’s Template ID assessed and since was inherited from it’s parent as an ID of 3 it will be changed to an ID of 4.

Using this approach you can control which templates are inherited to at least the grandchild level. I suppose it would be possible to continue this approach as long as each new level is set to inherit a unique Template ID.