We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51292
    • 18 Posts
    I have a use case where I need to update a few template variables on some resources via an imported CSV, and I only want to update the template variables I put in the CSV, and leave everything else on the resource alone. I tried to use ImportX, however it replaces all of the fields that you didn't pass, so the resource loses its parent, alias, and all of the other template variables that were not passed in the CSV. My question to you folks is if anyone has written a plugin (or modified ImportX) that does this, or should I just write it myself/modify ImportX?

    This question has been answered by dallen. See the first response.

      • 3749
      • 24,544 Posts
      This would have to be custom code or a modification of ImportX. Do you have control over the format of the CSV file?

      It would be a very simple snippet if the format were something like this:

      Resource, TV, Value
      Resource, TV, Value
      etc.

      It would run significantly faster if the Resource and TV fields contained IDs, but you could also use pagetitle and name for them.

      If they were IDs, it should be as simple as this (untested):

      $lines = file('path_to_csv_file', FILE_SKIP_EMPTY_LINES);
      
      $count = 0;
      $output = '';
      foreach ($lines as $lineNumber => $value) {
          $data = explode(',', $value);
          if (count($data) != 3) {
              $output .= "<br />" . 'Mis-formatted line: ' . $value;
              continue;
          }
          $resource = (int)trim($value[0]);
          $tv = (int)trim($value[1]);
          $val = trim($value[2]);
      
          $tvr = $modx->getObject('modTemplateVarResource',
              array(
                  'resourceid' => $resource,
                  'tmplvarid' => $tv
              )
          );
          if (!$tvr) {
              $output .= '<br />' . 'Failed to get TVR: ' . $resource . ' -- ' . $tv;
              continue;
          } else {
              $tvr->set('value', $val);
              $tvr->save();
              $count++;
          }
      }
      
      $output .= '<br /><br />Updated ' . $count . 'TVs';
      return $output;
      [ed. note: BobRay last edited this post 10 years, 6 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • discuss.answer
        • 51292
        • 18 Posts
        Thanks for your reply. It turns out that this was actually a core modx problem, and not an importx problem. It was setting the values of template variables that weren't passed to "", instead of just ignoring them like it should have.

        What I did was added a new processor that extends the existing processor and overrides the saveTemplateVariables function. Here is the new processor: https://gist.github.com/doctorallen/79af32bc4096531d50af. I put this in /core/model/mox/processors/resource/. I then set the importx.processor setting to updatenooverride. You still have to pass the alias, parent, and published settings in the CSV, because apparently those get overridden anyway and I didn't care to try and fix that as well.

        Here is the example CSV I used:

        id;alias;parent;published;tv1
        2;test-alias;18;1;value1


        Here's the GitHub ticket: https://github.com/Mark-H/importX/issues/64#issuecomment-201180816
          • 3749
          • 24,544 Posts
          Looks good. I'm glad you got it sorted. smiley
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting