Cleaning old TV value when ressource template change

Hi,
I dont know if i am really clear on this on :
in production i may have to change template and tv many time, assigning tv and unassign them to template … and i found some of my code working on a server and not on another one

i check the result of TV of some ressources and it seem tv dont get deleted when you change ressource template .
how i can delete old tv value on my ressource when i assigne a new template ?
do i have to do this in database ?
am i do it in a wrong way ?

1 Like

Do you mean that you can still see the TV when editing a resource even though the TV is no longer connected to the Resource’s Template?

If that’s the case, it sounds like a MODX or browser cache issue, or some third-party caching software like Varnish.

If that’s not what you mean, please explain further.

1 Like

i have 2 template with 1 tv each (multiple selection list)
i make a sort on ressource for 1 item of the list for those 2 tv
for some of my ressources when i use the template 1 and the first item of the list tv show an empty value… and when i switch to the template 2 … it works

TV 1:
Sélectionnée pour l’émission==1||Incubée==2||Sélectionnée pour la finale==3||En finale==4||Gagnante==5||Archivée==6

TV 2:
Incubée==2||Sélectionnée pour la finale==3||En finale==4||Gagnante==5||Archivée==6

those tv are assign on different template
but for some reason, when i change the template for some ressource (not all) i can’t sort with number 2 (incubée) … when i make a snippet to print the tv value it say it is empty … when i change the template it works, when i change the value( 3,4,5,…) it works… and for some ressources i have no problem at all.
i think the ressources who dont work are the very first ressources i made, i modify tv and template a lot so maybe old tv value are stored somewhere and make those ressources react weird. i can’t explain that.

1 Like

I think that’s a good theory. If you’ve changed the TV at all, the ones you created before the change may be stored in a different format.

The TV values are stored in the database as modTemplateVarResource objects in the modx_site_tmplvar_contentvalues table.

In each row of the table, the value field holds the value of the TV for a specific resource. The other two important fields are:

`tmplvarid`  (integer - ID of the TV)
`contentid`  (integer -- ID of the Resource)

The earliest ones you created about probably at the top of the table.

TV’s that are set to their default value won’t have a record in this table.

2 Likes

I don’t think, deleting existing TV-values, when you change the template by default, would be a good idea, but you could create a plugin, which could do that at OnDocFormSave

1 Like

To add to what bruno17 wrote, if you’re constantly changing page templates back and forth, there’s probably a much easier way to do what you’re trying to accomplish. We could probably be more help if you can explain what you’re trying to do and why.

The only time I ever change a page’s template is when I’ve created a better template to replace an existing one.

1 Like

Thanks to give me a way to understand the “underground” of modx. Well, may be i use template the wrong way. in my case i have to explain my situation a little bit : i made a website for a startup incubator who make a youtube show on industrial innovations. New startup can join the incubator and chose to participate or not to the show. There are my 2 differents templates, to sort and apply different TV on ressources Startup, which are slightly different : one for startup who decide to enter the show, and another for those who dont. Theses templates are applyed at the ressources creation, driven by a form … but human are inconstant and i have to check if this choice can be reversed. So i will tell to the community manager who have to change this setting to “clean” TV before change template : just deselect all selection on a multi selection TV before changing template. I think it’s a special case who dont needed to develop specific tool

1 Like

Ok very good to tell us more.

It sounds to me like you should just execute a deletion of the tv upon user selection, right?

That might be more direct and easier, ultimately.

Glad you like Modx, let’s go! :slight_smile:

Looking at your post, you want the users to be able to switch to the other track? If that’s the case you would want to define those groups and give them different templates as you have now. Or you could define one or more groups and leave the last group as basic, which is (web) group i believe.

Then, the switch on user selection will be switch to the one track, or the other. That could be a custom snippet, but its a simple bit of code tbh. If you know the right valve to open lol.

You could also delete tvs upon this action.

You won’t have to clear tvs this way, if you don’t display the tvs then they are effectively not there. You could possibly retain that data while provlding no method for the user to access them.

1 Like

bump for update on possible configurations, excellent test case!! :laughing:

Would it be possible to use a single template, but with different chunks for your two conditions?

That would make things a lot simpler.

You could put the users in separate user groups as nuan88 suggests, since the decision to participate is a user decision. You could also create a User Setting to store the user’s decision.

For example, the User Setting (let’s call it “show_or_not”) could contain the values “show” or “noshow.”

The chunk names could have the prefixes ‘show’ and ‘noshow’. That way, the correct chunk could be displayed with a tag like this:

[[$[[++show_or_not]]chunkname]]

The tag above is a chunk tag with a system setting tag inside it that sets the prefix on the chunk name based on the user setting.

Now, all you have to do is change the value of the User Setting. You can still have your TVs if you need them, but just not show them in the one chunk or the other.

If you use a User Group (there may be other advantages to that), you can write a simple snippet to select the chunk to be displayed. Something like this:

if ($modx->user->isMember('showGroup')) {
     $chunkName = 'show' . 'chunkName';
} else {
     $chunkName = 'noshow' . 'chunkName';
}

return $modx->getChunk('$chunkName);
1 Like

BobRay, would it be cool to ask what the user-selected ‘converter’ would look like? Seems to me just a snippet that will get the current user and change their status…it could run in a very simple form

That sounds about right to me. Just a form with a checkbox and a snippet to change the value based on the $_POST value if it doesn’t already match.

1 Like

I see what do you mean. Your answer make me think about getting more practice on user group. For this problem, there is pros and cons. Most of my choices come to make edition easyer for editor. One functionnality i use with template is the icone one. Very usefull to identify wich startup ressource are going to the show or not. My community manager can visualize in a second the right group of stratup, specific TV are made for him again : he can’t select the wrong possibility in the list because i made specific TV that match specification of each group (and here is the begining of the problem). Well, it’s not a problem on the “show item” side of code, more in day-use for non-programmer user. If switching people begin to be more than 2 or 3 a month, i will made a tool to make this switch nice and clean.

1 Like

You could keep the most basic user level as web users, its the default. Then a special level for those who join the event.

With chunks you will only give people the right pieces

TVs would be good here, on the joined people side especially, usually tvs would be things like lists of information