Search code examples
phpmodxmodx-revolution

Programatically retrieve TVs assigned to a MODX Resource by a given ID


I'm rather new to MODX. I'm passing in a resource ID, and with that, I'd like to programatically retrieve any custom TVs used by a given resource along with its value. Here's where I've gotten so far:

$resourceId = 1;
$resource = $modx->getObject('modResource', $resourceId);

/ * @TODO How do I grab any dynamically created TVs/Values? */
$array[] = [
    'id' => $resource->id,
    'pagetitle' => $resource->pagetitle,
    'alias' => $resource->alias
];

Thank you!


Solution

  • I solved my own problem with some help from this resource: https://bobsguides.com/revolution-objects.html For those of you interested, here is what I ended up with:

    $resourceId = 1;   
    
    $resource = $modx->getObject('modResource', $resourceId);
    
    $tvs = $resource->getMany('TemplateVars');
    
    $array[] = [
        'id' => $resource->id,
        'pagetitle' => $resource->pagetitle,
        'alias' => $resource->alias,
    ];
    
    foreach($tvs as $tv) {
        $array[0][$tv->name] = $tv->value;
    }