Try this. It’s a little shorter:
$tvValue = $modx->resource->getTVValue('foo');
(note the three capital letters in getTVValue).
I’m assuming that you want the value for the current resource. If not, it’s a little more complicated, but not much. It depends on what information you have to begin with.
Ok, thanks BobRay,
but what if I want it from another Resource.
And I have either the Resource-Id or the Resource (handle?) in a variable?
Fact is that the docs don’t have these kind of simple HowTo’s
Would be a big help for API programmers.
Thanks in advance for your help.
Bert
It looks good, although this might be a little faster if you don’t have the TV object or the Resource object already. It should be more efficient because TVs are smaller than resources. I’m assuming that you have both the resource ID and the TV ID. Using IDs is also a little more efficient than using the names.
$tv = $modx->getObject('modTemplateVar', $tvId);
$val = $tv->getValue($resourceId);
or, if you need the processed value rather than the raw value of the TV (if it’s just a string or integer, you don’t need this):
$val = $tv->renderOutput($resourceId);
This question is probably only loosely related to the initial one, but I found this thread during searching for a solution to my problem, which is the following:
I'd like to set a default value for a tv individually per resource ie the default_text field of a tv object, but it doesn't seem to be possible because the default_text field is specified globally for a tv, so the default_text/default value is only settable for each resource as the same value...I think that's the nature of this field...is this right or is there any way to achieve this and if not, would that probably be a feature request for a future release?
You're correct. I'm not sure it makes sense as a feature request because you can already set the value of a TV for each resource. Having a default for each resource would mean having to set that default value for all the resources, in which case, you might as well set the value in each resource to begin with. Maybe I'm misunderstanding you.
You might be able to do part of what you want with a default value of @INHERIT or @INHERIT somevalue.
In the first case, the default value is set to match the value of the closest ancestor with the value set. In the second, the same thing except that if no ancestor has the value set, the value is 'somevalue'.
In either case, you can set a default value for a subset of resources by putting them under the same container in the tree.
You can also use Form Customization to set the default value based on some criterion. You could have a default value for each individual resource, but you'd need a Form Customization rule for every resource.
Actually setting the tv value with a snippet called with @EVAL in the input options is what I'm currently doing, it works well but I'm using it with the checkboxsortable tv type and the problem is, that I have to reload the page once again to see if something changed...seems that modx is not reading the tv values again OnTVRenderInputList (some strange event that is not in the docs and somehow also not invoked (but in modxtemplatevar.class.php) when their are changed by the @EVAL snippet...
anyways, it works, not perfectly but also not far from it =)...