Force MODX to save a TV's default value to database

It seems that when a Template Variable has a default value, that default value is not stored in the database table: site_tmplvar_contentvalues for each page. Rather I am guessing it gets grabbed from: site_tmplvar at build time, whenever an alternate value was not entered for that page?

Is there a way to force MODX to save the TV even when it will have the default value? We would like to be able to access this data directly from the database for external PHP build functions.

I don’t think this is possible.

Maybe you could write a custom plugin for the event OnDocFormSave that adds all the missing database entries (but you would work against the system as every time you’d save a resource, rows from modx_site_tmplvar_contentvalues would be deleted that you had to create then again).

One solution might be to set a dummy default value that would never be used (e.g, ‘XXX’), then create a plugin that would automatically set the TV’s value to the “real” default value in, maybe in OnDocFormSave (or maybe OnBeforeDocFormSave) if it’s attached to the resource’s template and no value is already set. MODX will only use the dummy default value if the TV has no value for the page.


At this point, if MODX doesn’t have that built in, then it is probably just as easy to set the default value in our build script and then replace it if the database returns a value.

to get the value of a TV including the default-value, you can use

if you need the processed value, use

which is also called by

Thanks. That looks more like an “inside of MODX” type solution, whereas our needs are for a totally independent system.

In a nutshell, we are using MODX as the M in JAMStack, generating static html pages to serve, plus json files for navigation by partial page hydration. And some of the TV values need to fetched independently as part of the json build process.

Well, at some point you need to build your stuff. Do you not use MODX to help with that?

Yes we use MODX to create the pages. Then a separate system fetches the pages, saves a copy and extracts the changed content, saving that separately. Then everything gets uploaded to a CDN.

Some of the TVs are used in determing some of the extraction and build steps, so it helps when they can be easily pulled.