$resources = array();
$resources[1] = $modx -> newObject('modResource');
$resources[1] -> fromArray(array(
'id' => 1,
'pagetitle' => 'Example Resource',
'parent' => 0,
'isfolder' => 0,
'hidemenu' => 0,
'alias' => 'example-resource',
'published' => 1,
'searchable' => 1,
'template' => /* how can I get the id of the template during installation? */,
'menuindex' => 0,
'longtitle' => 'Example Resource',
'description' => 'Example Resource Template',
'introtext' => 'Example Resource Template',
'content' => /* large part of mixed HTML + snippet calls doesnt fit here very well */
), '', true, true, false);
return $resources;
This question has been answered by BobRay. See the first response.
{pagetitle}.content.html$resources[1] = $modx->newObject('modResource');
$resources[1]->fromArray(array (
'id' => 1,
'type' => 'document',
'contentType' => 'text/html',
'pagetitle' => 'Resource1',
'longtitle' => '',
'description' => '',
'alias' => 'resource1',
'link_attributes' => '',
'published' => false,
'isfolder' => true,
'introtext' => NULL,
'richtext' => false,
'template' => 'default',
'menuindex' => 0,
'searchable' => true,
'cacheable' => true,
'createdby' => 0,
'editedby' => 0,
'deleted' => false,
'deletedon' => 0,
'deletedby' => 0,
'menutitle' => '',
'donthit' => false,
'privateweb' => false,
'privatemgr' => false,
'content_dispo' => 0,
'hidemenu' => false,
'class_key' => 'modDocument',
'context_key' => 'example',
'content_type' => 1,
'hide_children_in_tree' => 0,
'show_in_tree' => 1,
'properties' => NULL,
), '', true, true);
$resources[1]->setContent(file_get_contents($sources['data'].'resources/resource1.content.html'));
if (!function_exists('checkFields')) {
function checkFields($required, $objectFields) {
global $modx;
$fields = explode(',', $required);
foreach ($fields as $field) {
if (! isset($objectFields[$field])) {
$modx->log(MODX::LOG_LEVEL_ERROR, '[Resource Resolver] Missing field: ' . $field);
return false;
}
}
return true;
}
}
if($object->xpdo) {
$modx =& $object->xpdo;
switch ($options[xPDOTransport::PACKAGE_ACTION]) {
case xPDOTransport::ACTION_INSTALL:
case xPDOTransport::ACTION_UPGRADE:
$intersects = array (
0 => array (
'pagetitle' => 'Resource1',
'parent' => 0,
'template' => 'default',
),
1 => array (
'pagetitle' => 'Resource2',
'parent' => 'Resource1',
'template' => 'Template2',
'tvValues' => array (
'Tv1' => 'SomeValue',
'Tv2' => 'SomeOtherValue',
),
),
);
if (is_array($intersects)) {
foreach ($intersects as $k => $fields) {
/* make sure we have all fields */
if (! checkFields('pagetitle,parent,template', $fields)) {
continue;
}
$resource = $modx->getObject('modResource', array('pagetitle' => $fields['pagetitle']));
if (! $resource) {
continue;
}
if ($fields['template'] == 'default') {
$resource->set('template', $modx->getOption('default_template'));
} else {
$templateObj = $modx->getObject('modTemplate', array('templatename' => $fields['template']));
if ($templateObj) {
$resource->set('template', $templateObj->get('id'));
} else {
$modx->log(MODX::LOG_LEVEL_ERROR, '[Resource Resolver] Could not find template: ' . $fields['template']);
}
}
if (!empty($fields['parent'])) {
if ($fields['parent'] != 'default') {
$parentObj = $modx->getObject('modResource', array('pagetitle' => $fields['parent']));
if ($parentObj) {
$resource->set('parent', $parentObj->get('id'));
} else {
$modx->log(MODX::LOG_LEVEL_ERROR, '[Resource Resolver] Could not find parent: ' . $fields['parent']);
}
}
}
if (isset($fields['tvValues'])) {
foreach($fields['tvValues'] as $tvName => $value) {
$resource->setTVValue($tvName, $value);
}
}
$resource->save();
}
}
break;
case xPDOTransport::ACTION_UNINSTALL:
break;
}
}
return true;if (isset($fields['tvValues'])) {
foreach($fields['tvValues'] as $tvName => $value) {
$resource->setTVValue($tvName, $value);
}
}if (isset($fields['tvValues'])) {
foreach($fields['tvValues'] as $tvName => $value) {
$get_resource = $modx->getObject('modResource', array('pagetitle' => $value));
$get_id->get($get_resource=>'id');
$resource->setTVValue($tvName, $get_id);
}
}