i have a hook that saves formit form to a resource.
all is working nicly however I can not seem to save tagger tags.
I have the following snippet
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget('FILE');
// Load the Tagger service
$tagger = $modx->getService('tagger', 'Tagger', $modx->getOption('tagger.core_path', null, $modx->getOption('core_path').'components/tagger/').'model/tagger/');
if (!$tagger) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load Tagger service.');
return false;
}
// Create new resource
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
$allFormFields = $hook->getValues();
// Set core fields
foreach ($allFormFields as $field => $value) {
if (in_array($field, ['pagetitle', 'content'])) {
$doc->set($field, $value);
}
}
// Set other resource properties
$doc->set('parent', 290);
$doc->set('alias', time());
$doc->set('template', '2');
$doc->set('published', '0');
if (!$doc->save()) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Failed to save resource');
return false;
}
$resourceId = $doc->get('id');
$modx->log(modX::LOG_LEVEL_ERROR, "Resource saved with ID: $resourceId");
// Loop to save template variables (TVs)
foreach ($allFormFields as $field => $value) {
if ($tv = $modx->getObject('modTemplateVar', ['name' => $field])) {
if (is_array($value)) {
$value = implode('||', $value);
}
$tv->setValue($resourceId, $value);
if (!$tv->save()) {
$modx->log(modX::LOG_LEVEL_ERROR, "Failed to save TV: $field for Resource ID: $resourceId");
}
}
}
// Add tags using Tagger for each tag type
$tagFields = [
'subject_tags' => 'subject_tagger_group_id',
'topic_tags' => 'topic_tagger_group_id',
'grade_tags' => 'grade_tagger_group_id',
'material_type_tags' => 'material_type_tagger_group_id',
'editor_tags' => 'editor_tagger_group_id',
'language_tags' => 'language_tagger_group_id',
];
foreach ($tagFields as $tagField => $groupField) {
if (!empty($allFormFields[$tagField]) && !empty($allFormFields[$groupField])) {
$tags = explode(',', $allFormFields[$tagField]);
$taggerGroupId = (int) $allFormFields[$groupField];
$modx->log(modX::LOG_LEVEL_ERROR, "Processing tags for $tagField in group $taggerGroupId");
foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
$tagObject = $modx->getObject('TaggerTag', [
'tag' => $tag,
'group' => $taggerGroupId,
]);
if (!$tagObject) {
$tagObject = $modx->newObject('TaggerTag');
$tagObject->set('tag', $tag);
$tagObject->set('group', $taggerGroupId);
if (!$tagObject->save()) {
$modx->log(modX::LOG_LEVEL_ERROR, "Failed to create Tag: $tag in Group: $taggerGroupId");
}
}
// Link the tag to the resource
if (!$tagObject->addMany($doc)) {
$modx->log(modX::LOG_LEVEL_ERROR, "Failed to link Tag: $tag to Resource ID: $resourceId");
} else {
$modx->log(modX::LOG_LEVEL_ERROR, "Linked Tag: $tag to Resource ID: $resourceId");
}
}
}
}
}
return true;
getting following errors
Processing tags for subject_tags in group 1
Could not load class: TaggerTag from mysql.taggertag
TaggerTag::load() is not a valid static method.
Could not load class: TaggerTag from mysql.taggertag
input form i got as an example
<input id="input-tag2" type="text" class="input-tags"
value="[[TaggerGetTags? &groups=`1` &rowTpl=`tag_tpl`]]"
autocomplete="off" name="subject_tags"
>
<input type="hidden" name="subject_tagger_group_id" value="1">