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

    I did some digging into the MODx database and API and found the modContextResource object and it’s related table (context_resource).

    Now I haven’t gotten round to creating any snippets or anything like that at the moment but there are two fields in this object/database table


    • context_key
    • resource

    So if, for instance, you have another context called web2 and a document with ID 13 that you want to share between contexts

    You could add a record with web2 in the context_key and 13 in the resource will mean that the document with ID 13 will be viewable by the web2 context but it will not appear in the list in the manager.

    I have tested this by manually created the records using phpmyadmin but as of yet have no modx snippets or addons to achieve this...
      • 6511
      • 60 Posts
      OK,

      I have created an extremely basic snippet which when ran will add a single resource to another context (it’s all hardcoded at the moment - only meant for an example)
      $docResource = $modx->newObject('modContextResource');  
      	  
      // now, lets save some data into the fields  
      $docResource->set('context_key','sst');  // replace sst with your context key for your new context 
      $docResource->set('resource', '13'); // replace 13 with your resource ID
      	  
      // okay, now we're ready. let's save.  
      if ($docResource->save() === false) {  
        die('An error occurred while saving!');  
      }

      This is still a manual process at this point though and you will need to adapt this to your situation but it gives you a quick demo of what to do....
        • 5357
        • 30 Posts
        After looking as the bug tracker, is this workaround the only way as a fix doesn’t look imminent?
          • 6511
          • 60 Posts
          I don’t know if there are any other ways but since then again this thread has been open since October 1st - you would have thought if there was an easy (easier) way it would have been posted by now.

          It’s the only way I have found to do it and that was only by digging through the modX database schema and extensive trial and error.

          I have put mine in a simple loop to add all the documents I need but haven’t got it checking for existence yet meaning that it errors when you run it more than once...
            • 6511
            • 60 Posts
            OK I have updated my above snippet to be a little more useful. It still isn’t the most efficient or elegant of scripts but it does the job:
            <?php
            
            if (!function_exists('add_resource_to_context')) 
            {
               function add_resource_to_context($docResource, $context_key , $resource_id )
                    {				
            		// now, lets save some data into the fields  
            		$docResource->set('context_key',$context_key);  
            		$docResource->set('resource', $resource_id);
            			  
            		// okay, now we're ready. let's save.  
            		if ($docResource->save() === false) {  
            			return 'Resource '.$resource_id.' was <b>NOT</b> successfully added to the '.$context_key.' context.<br />';
            		} else {
            			return 'Resource '.$resource_id.' was successfully added to the '.$context_key.' context.<br />';
            		}
            	}
            }
            
            
            $containerArray = array (13,62,88,90);
            $contextArray = array('sst');
            
            $resources = $modx->getCollection('modResource');  
            foreach ($resources as $resource) 
            {  
            
            	foreach ($containerArray as $parentId) 
            	{
            		if ($resource->get('parent') == $parentId || $resource->get('id') == $parentId)
            		{
            			foreach ($contextArray as $context) 
            			{
            				echo add_resource_to_context($modx->newObject('modContextResource'), $context, $resource->get('id'));
            			}
            		}
            	}
            
            }


            It loops through an array of resources and adds itself and any direct children it has to all of the contexts in an array. It relies on the database constraints to prevent duplicates so I am afraid you will see some ugly database errors in there when you run it as it comes across duplicates. My advise is to run it as an admin function when you want to update the cross domain content and make it an admin access only link...

            Hope this helps...
              • 5357
              • 30 Posts
              There’s obviously more to the fix by the looks of it and maybe there aren’t many people using more than one context and across sub domains.

              Would rather not have to resort to workarounds as I could simply just duplicate the symlinked content but then that just defeats the whole concept of a CMS.
                • 6511
                • 60 Posts
                I know what your saying however, the database schema to do this is there already.

                That means that on some level they planned for a resource to be available over multiple contexts - at the moment they just don’t have a way for you to do this through the manager...

                I am using the modX API to do it. I am only using the tools the modX framework has in place. Yes I will admit it certainly isn’t a finished bit of work or well polished but it does the job. I have potentially hundreds of resources which need to be shared between contexts duplicating documents isn’t an option especially for the pages that might be edited later down the line.

                  • 22826
                  • 9 Posts
                  Sorry for the late reply on this. But the best solution that we came up with was to amend the core in /core/model/modx/modrequest.class.php, what was finally done can be found here http://www.saunderswebsolutions.com/articlecontent/11, as kevin says it is not ideal to amend the core, but at the time it looked like the best way to allow symlinking across contexts.
                    • 25663 MODX Staff
                    • 12,272 Posts
                    This should be resolved I believe in 2.1, meaning no hacks will be needed. It would be great to get verification on it working as you expect paulk.
                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 22826
                      • 9 Posts
                      That is great rthrash, I’ll do an upgrade now to check.. thank you