Setting TV default value to the value of a different TV

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.

1 Like

:thinking: :confused:
not sure, to understand, what you are asking and are you talking about frontend or the manager?

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.

First off, front end or backend matters.

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.

$topProductValue = $resource->getTVValue('TopProduct');
$allProductsValue = $resource->getTVValue('AllProducts');

if ( ($topProductValue !== null) && ($allProductsValue !== null) )  {
    if (empty($topProductValue) ) {
        $resource->setTVValue('TopProduct', $allProductsValue);
    }
}
return;

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;
}
1 Like

Thanks bobray. So there is no built in way to do this, it has to be a customization?

And yes, based on your suggestion, you seem to understand what I was aiming for.

Right. Itā€™s kind of an unusual operation.

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). :wink:

1 Like

Sorry, my code above was missing a semicolon, so if you tried it, it crashed. Iā€™ve edited the earlier post to fix it.

Thanks for the correction.

Donā€™t thank me until it works. :wink:

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.

But do thank you for your help overall.

1 Like

Right, that sounds much more efficient