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

    Just been busy beavering away on MODX. I have a multilingual/multi-context site setup and the bit that has seemed not quite right about this process for years is that you must manually add the context_settings to each context.

    Is there a way of using an event (e.g. OnContextSave) within a plugin to predefine the context settings each time a new context is added?

    So for example I would add a new context (e.g. "french") and it would automatically know to add some default settings (probably just duplicating the master config settings) such as:


    • base_url
    • site_url
    • site_start
    • cultureKey

    It would simply be a case then for admin users to hop into the context settings and quickly modify the values only.

    Has anyone done this? Have I completely missed something?!

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

      Digital bod.
      • 45235
      • 6 Posts
      Couldn't you just create a prototype context in the manager and then duplicate that? If you duplicate a context in the resource tree that will retain your settings and duplicate any child pages. So after creating the default context you could write a snippet that would duplicate it for you:

      https://forums.modx.com/thread/85186/duplicate-context-with-xpdo
        • 33971
        • 12 Posts
        Hey spronych, thanks for the suggestion and this is something that I have done that in the past, but it's just not a particularly elegant way of doing it.

        When opening the MODX manager to other admins it would be much nicer to just let them create a context using the context management section safe in the knowledge that it will work "out of the tin". We are building a platform that will allow clients to be their own admins and we don't want them to have to hack at all.

        It would be more correct to either "clone" an existing context (as per your suggestion) and/or simply create one from scratch. This would mean that no matter where you developed your contexts from, it would work. This is one of the benefits of MODX Events as it should be a case of listening out for the event and then that will work across the stack OnContextSave.

        It would also allow an option to build a CMP that could simply ask the users which contexts they want (imagine a site in 23 languages) and then it could auto-generate the contexts. They can then use Babel to clone resources.

        Just found an gonna try with references from: http://rtfm.modx.com/revolution/2.x/administering-your-site/settings/system-settings#SystemSettings-SavingaSystemSetting%28programmatically [ed. note: wiseguydigital last edited this post 12 years, 10 months ago.]
          Digital bod.
        • discuss.answer
          • 33971
          • 12 Posts
          I have it working!

          In a plugin with OnContextSave as the Event use something along these lines: (This is pseudo code as we have more in our plugin...)

          <?php
          if ($modx->event->name == 'OnContextSave') {
          
            $setting = $modx->newObject('modContextSetting');
            $setting->set('context_key', 'my_new_context');
            $setting->set('key', 'base_url');
            $setting->set('value', '/');
            $setting->set('xtype', 'textfield');
            $setting->set('namespace', 'core');
            $setting->set('area', 'Lexicon and Language');
            $setting->save();
          
            // Clear the cache:
            $modx->cacheManager->refresh(array(
               'context_settings' => array('contexts' => array('my_new_context'))
            ));
          
          }
          

          You would want to check for the existence of the context and settings first for instance, and is this an update or new event etc. But fundamentally it works.
            Digital bod.
            • 3749
            • 24,544 Posts
            Once you have them all set, you can add this at the top of the plugin so it will only act on new ones:

            if ($modx == modSystemEvent::MODE_UPD) return;


            Also, if OnContextSave is the only event the plugin is connected to, there's no need for the "if" statement at the top, since it will always be true.
              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
              • 33971
              • 12 Posts
              Quote from: BobRay at Nov 08, 2013, 02:13 AM
              Once you have them all set, you can add this at the top of the plugin so it will only act on new ones:

              if ($modx == modSystemEvent::MODE_UPD) return;


              Also, if OnContextSave is the only event the plugin is connected to, there's no need for the "if" statement at the top, since it will always be true.

              Thanks BobRay,

              We are using the MODE_UPD param too in our plugin. That reminds me that I meant to add the current API (2.2) link should anyone have the same issues:

              http://api.modx.com/revolution/2.2/db_core_model_modx_modcontextsetting.class.html#%5CmodContextSetting

              Personally I think it's good practice to add the event type checking whether or not you are only listening to one event. It's a fail-safe and should you want to expand your plugin with another event then you reduce the likelihood of calling the wrong actions. Personal preference I guess smiley

              When I have this all working, I'll post it up as it's a really nice way of making multi-lingual sites easily.
                Digital bod.
                • 3749
                • 24,544 Posts
                There's not much harm in checking the event name in a plugin attached to a save event, but I wouldn't do it for something like OnWebPagePrerender since it would slow down page loads slightly for the whole site.

                I can be forgetful, but I don't think I'd ever forget to add a check for the event name when adding more events to a plugin. YMMV. wink
                  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