We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40045
    • 534 Posts
    I really tried hard to find out if this is possible, but after countless fatal erros I give up on this =)...

    What I'm trying to do is get an array of all elements that are static ie. have a file as their source (static_file != emtpy...)

    I can get for example the path of the static file of a defined template with

    $elements = $modx->getObject('modTemplate', 1);
    return $elements->static_file;
    


    but this is very unflexible...what I need is something that gives me 1. not only the template with ID of 1 (but every existing template > this will be used in a plugin and I don't know how many templates there will be...) and 2. not only templates but all elements that have a static file as source... I tried
    $modx->getCollection
    and
    $modx->getIterator
    but I wasn't able to get any output that comes even close to what I need (most if it were errors, probably due my lacking php skills...)

    is there a clever or better easy way to do that?
      • 33968
      • 863 Posts
      Well, my first thought was to use getCollection but you'll have to run it for each element type. Or get them all in one query if you need it to be more efficient.

      $output = array();
      
      $elementTypes = array(
          'modTemplate',
          'modChunk',
          'modSnippet',
          'modPlugin'
      );
      
      foreach ($elementTypes as $type) {
          $staticElements = $modx->getCollection($type, array('static' => 1));
          foreach ($staticElements as $element) {
              $output[$type] = array(
                  'id' => $element->get('id'), 
                  'path' => $element->get('static_file')
              );
          }
      }