$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!');
}<?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'));
}
}
}
}