you can also collect all tags in a custom-table with a plugin like that:
<?php
/**
* helper method for missing params in events
* @param modX $modx
* @param array $children
* @param id $parent
* @return boolean
*/
function tagManGetChildren(&$modx, &$children, $parent) {
$success = false;
$kids = $modx->getCollection('modResource', array('parent' => $parent, ));
if (!empty($kids)) {
foreach ($kids as $kid) {
$children[] = $kid->toArray();
tagManGetChildren($modx, $children, $kid->get('id'));
}
}
return $success;
}
function tagManSetTagsArray(&$modx, $tvname, $resourceArray) {
if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
$tagsArray = array();
$tagsArray['add_tags'] = $resourceArray['deleted'] == 0 ? true : false;
$tagsArray['id'] = $resourceArray['id'];
$tagsArray['tags'] = $tv->renderOutput($resourceArray['id']);
$modx->log(modX::LOG_LEVEL_DEBUG, 'Indexing ' . $tv->get('name') . ': ' . $tagsArray['tags']);
return $tagsArray;
}
return false;
}
$tvname = 'migxtagmanager_tags';
$action = 'activate';
$resourcesToIndex = array();
switch ($modx->event->name) {
case 'OnDocFormSave':
$resourceArray = $resource->toArray();
if ($tagsArray = tagManSetTagsArray($modx, $tvname, $resourceArray)) {
$resourcesToIndex[] = $tagsArray;
}
break;
case 'OnResourceDuplicate':
$resourceArray = $newResource->toArray();
if ($tagsArray = tagManSetTagsArray($modx, $tvname, $resourceArray)) {
$resourcesToIndex[] = $tagsArray;
}
$children = array();
tagManGetChildren($modx, $children, $resourceArray['id']);
foreach ($children as $child) {
if ($tagsArray = tagManSetTagsArray($modx, $tvname, $child)) {
$resourcesToIndex[] = $tagsArray;
}
}
break;
case 'OnResourceDelete':
case 'OnResourceUndelete':
$resourceArray = $resource->toArray();
if ($tagsArray = tagManSetTagsArray($modx, $tvname, $resourceArray)) {
$resourcesToIndex[] = $tagsArray;
}
$children = array();
tagManGetChildren($modx, $children, $resourceArray['id']);
foreach ($children as $child) {
if ($tagsArray = tagManSetTagsArray($modx, $tvname, $child)) {
$resourcesToIndex[] = $tagsArray;
}
}
break;
}
if (count($resourcesToIndex) > 0) {
$packageName = 'migxtagmanager';
$classname = 'mtmResourceTags';
$packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
$modelpath = $packagepath . 'model/';
if (is_dir($modelpath)) {
$modx->addPackage($packageName, $modelpath, $prefix);
foreach ($resourcesToIndex as $resourceArray) {
$tags = $modx->getOption('tags', $resourceArray, '');
$id = $modx->getOption('id', $resourceArray, 0);
$add_tags = $modx->getOption('add_tags', $resourceArray, false);
if (!empty($id)) {
$tags = explode(',', str_replace('||', ',', $tags));
//get existing tags
$existing = array();
$c = $modx->newQuery($classname, array('resource_id' => $id));
if ($collection = $modx->getCollection($classname, $c)) {
foreach ($collection as $object) {
$existing[$object->get('tag')] = $object;
}
}
if ($add_tags) {
foreach ($tags as $tag) {
if (isset($existing[$tag])) {
} else {
$object = $modx->newObject($classname);
$object->set('tag', $tag);
$object->set('resource_id', $id);
$object->save();
}
unset($existing[$tag]);
}
}
foreach ($existing as $object) {
$object->remove();
}
}
}
}
}
return;
the xpdo-schema for the table:
<?xml version="1.0" encoding="UTF-8"?>
<model package="migxtagmanager" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
<object class="mtmResourceTags" table="migxtagmanager_tags" extends="xPDOSimpleObject">
<field key="tag" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
<field key="resource_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" index="index" />
<aggregate alias="Resource" class="modResource" local="resource_id" foreign="id" cardinality="one" owner="foreign" />
</object>
</model>
this is in a package named 'migxtagmanager'
you can create this package-directory and the classes and table in some minutes with help of MIGX
Than you can get the Tags with:
[[!migxLoopCollection?
&packageName=`migxtagmanager`
&classname=`mtmResourceTags`
&joins=`[{"alias":"Resource","selectfields":"id"}]`
&where =`{"tag:LIKE":"B%","Resource.published":"1"}`
&groupby = `tag`
&sortConfig = `[{"sortby":"tag"}]`
]]
don't forget to change the TV-name in this line:
$tvname = 'migxtagmanager_tags';
[ed. note: Bruno17 last edited this post 12 years, 11 months ago.]