Is there a way to set the default value of a TV to be the entered value of another TV?
We have what is essentially a category template with a TV listing the product id numbers. A new snippet function will be limited to less than four products, so for categories with less than four, we would use the original string, but for categories with more we would create a select list of four.
While I could build a check tv2 if not use tv1 function into the snippet, I am wondering if my question is possible.
I am referring to Template Variables, which allow a default value. I am wanting to set the value of TV called TopProduct equal to TV called AllProducts, unless I have entered a different value into TV TopProduct. So the default value of TV TopProduct would be equal the current TV AllProducts for that Resource.
Do you want to change that value in the database, on the backend or do you simply want to show a different value on the front end? Here it sounds like you want to fix the backend value of the tv, unless its already got a value.
I think you will need a php snippet to do this? Just spitballing here.
Iām afraid Iām still not understanding you. TVs only have one default value regardless of the Resource. So changing the default value of TopProduct would affect the values used for that TV for all Resources that do not have a value set for TopProduct. I donāt think thatās what you want, but I could be wrong.
Itās quite doable to set the value of TopProduct (if itās empty) to the value of AllProducts for the current resource in a plugin attached to OnDocFormSave, but that would work best if the default value for TopProduct was empty, imo.
Wait, so you could insert a value into the second tv under some conditions, and that could be only for the tv for that resource I assume.
I think this is what OP wants, rather than that value being designated as a ādefaultā value in any programming sense.
OP, I think, just wants to insert a value when there isnāt one, seems to me. In some ways its a default value, but thatās just semantics I think, ie just a frontend replacement
Sorry if my terminology is incorrect, I am just a casual user of MODX not a trained developer.
Basically I want the value of the first TV to automatically be inserted into the second TV, but if I manually enter something into the second TV it would retain that manually entered value (same basic function a TV ādefaultā value would have).
If Iām understanding you, and Iām not sure I am, if (and only if) thereās no value for TopProduct when you save the resource, you want the value from AllProducts to be used for it. If there is a value for TopProduct, that value will be used instead.
If thatās the case, this plugin attached to OnDocFormSave might do the job.
The two tests for null are to keep the plugin from operating for resources that donāt have those TVs (for example, the TVs are not connected to the resourceās template).
If thereās a good way to identify the specific resources you want this to run on (e.g., they all have the same parent), you could make this a little faster by adding a test at the top like this:
if ($resource->get('parent') != 12) { /* replace 12 with the ID of the parent resource */
return;
}
Just create a Plugin (name it whatever you want), paste in the code, and on the āSystem Eventsā tab, put a check next to OnDocFormSave, save the plugin, and you should be in business (assuming that my untested code works).
Actually I am thinking to take a different approach and test for the TV during the build process for the static website. That way I can add values to the few resources that need them and not have to go through every existing resource and resave it to run the plugin idea.