We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • So I add ClientConfig settings on package install using a resolver with elements like this:

    $analyticsSettings = $modx->getObject('cgGroup', array('key' => 'analyticsSettings'));
    if (!$analyticsSettings) { 
        $analyticsSettings = $modx->newObject('cgGroup');
        $analyticsSettings->fromArray(array(
            'label' => 'Google Analytics',
            'sortorder' => '20',
            'description' => 'Settings for Google Analytics SEO'
        ));
        $analyticsSettings->save();
    }
    
    $analytics_code = $modx->getObject('cgSetting', array('key' => 'analytics_code'));
    if (!$analytics_code) { 
        $analytics_code = $modx->newObject('cgSetting');
        $analytics_code->fromArray(array(
            'key' => 'cosmos.analytics_code',
            'label' => 'Analytics code',
            'xtype' => 'textfield',
            'description' => 'Fill in your tracking code (like: UA-10989210-17)',
            'is_required' => '0',
            'sortorder' => '1',
            'value' => '',
            'group' => $analyticsSettings->get("id")
        ));
        $analytics_code->save();
    }
    


    It work on first install, but when somebody updates the package the same values get installed again...

    How can I check if a setting already exists and NOT update or create a duplicate of it?
      MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
      DESIGNfromWITHIN, MPThemes and Any Screen Size
      Follow me on Twitter | Read my blog | My code on GitHub
      • 4172
      • 5,888 Posts
      you are searching for
      $modx->getObject('cgGroup', array('key' => 'analyticsSettings'));


      but you do not set a field with name key


      and here, you are searching for
      $modx->getObject('cgSetting', array('key' => 'analytics_code'));


      but you are setting the field 'key' with the value 'cosmos.analytics_code'
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • Quote from: Bruno17 at Dec 12, 2014, 02:30 PM
        you are searching for
        $modx->getObject('cgGroup', array('key' => 'analyticsSettings'));


        but you do not set a field with name key


        and here, you are searching for
        $modx->getObject('cgSetting', array('key' => 'analytics_code'));


        but you are setting the field 'key' with the value 'cosmos.analytics_code'

        Ahhh yes...
        So it should be like this:

        $analyticsSettings = $modx->getObject('cgGroup', array('label' => 'Google Analytics'));
          if (!$analyticsSettings) { 
            $analyticsSettings = $modx->newObject('cgGroup');
            $analyticsSettings->fromArray(array(
              'label' => 'Google Analytics',
              'sortorder' => '20',
              'description' => 'Settings for Google Analytics SEO'
            ));
            $analyticsSettings->save();
          }
        
        
          $analytics_code = $modx->getObject('cgSetting', array('key' => 'cosmos.analytics_code'));
          if (!$analytics_code) { 
            $analytics_code = $modx->newObject('cgSetting');
            $analytics_code->fromArray(array(
              'key' => 'cosmos.analytics_code',
              'label' => 'Analytics code',
              'xtype' => 'textfield',
              'description' => 'Fill in your tracking code (like: UA-10989210-17)',
              'is_required' => '0',
              'sortorder' => '1',
              'value' => '',
              'group' => $analyticsSettings->get("id")
            ));
            $analytics_code->save();
          }
        }
        
          MODX Ambassador (NL) | Responsive web design specialist, developer & speaker
          DESIGNfromWITHIN, MPThemes and Any Screen Size
          Follow me on Twitter | Read my blog | My code on GitHub