We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22629
    • 194 Posts
    Hi,

    I’m using $modx->getCollection() to query for a collection of resources in a snippet. I then convert the resource to an array using toArray() and pass into a chunk. However, I cannot retrieve the value of a TV called "main_image" on this resource using [[*main_image]] or [[+main_image]] in the chunk.

    How can I include the TV values in toArray() so I can reference them in a chunk. I’ve cut this code down a bit but it shows the meat and bones of it - it was inspired by the templating your snippets page in the modx documentation (

    $query = $modx->newQuery('modResource');
    $query->where(Array(
        'hidemenu' => 0,
        'published' => true,
        'deleted' => false
    ));
    
    // Get the resources
    $resources = $modx->getCollection('modResource', $query);
    
    $itemOutput = '';
    foreach ($resources as $resource)
    {
        $resourceArray = $resource->toArray();
        var_dump($resourceArray); <-- TV is not present in this array
        $itemOutput .= $modx->getChunk($itemTemplate, $resourceArray); <-- chunk contains [[+main_image]] but it is rendered blank
    }
    


    My environment is modx 2.0.4-pl2 on FreeBSD 8.1, Apache 2.2.16, MariaDB/MySQL 5.1.50, PHP 5.3.3.

    Thanks!

    Andy
      Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

      modx Revolution 2.2.6
      Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

      Content-Managed Websites Built on MODX
      • 28215
      • 4,149 Posts
      Getting TVs in a Resource is a bit more complex, since there are a possible unlimited # of TVs. However, you can do so in your loop:

      foreach ($resources as $resource) {
              $templateVars =& $resource->getMany('TemplateVars');
              foreach ($templateVars as $tvId => $templateVar) {
                  $tvs[$templateVar->get('name')] = $templateVar->get('value');
              }
          $resourceArray = array_merge($resource->toArray(),$tvs);
          $itemOutput .= $modx->getChunk($itemTemplate, $resourceArray);
      }
      


      If you want to process those TVs (ie, process any tags within them, but it takes up more processing time and memory), you can do:
      $tvs[$templateVar->get('name')] = $templateVar->renderOutput($resource->get('id'));
      


      instead. If the whole purpose of your snippet is to iterate over Resources, though, I’d recommend using the getResources Extra, as it’ll be far better and more up-to-date than most ad-hoc snippets.

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 22629
        • 194 Posts
        Hi Shaun,

        That worked brilliant, thanks.

        It is part of a larger snippet that builds up a large section of my home page - I just cut it down enough to show the issue - but thanks for the tip about getResources, I’ll take a look into it.

        Andy
          Andy Shellam | www.networkmail.eu | @Pandy06269 @NetworkMail

          modx Revolution 2.2.6
          Windows 2012 | IIS 8 | php 5.4.11 | MySQL 5.5.29

          Content-Managed Websites Built on MODX