Skip to content

Commit a4abc05

Browse files
committed
Allow tag links to include all current tags
Refactored the link generation to allow for current tags to be used as part of generating the url.
1 parent 280f489 commit a4abc05

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

core/components/tagger/elements/snippets/taggergettags.snippet.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* &showUnused int optional If 1 is set, Tags that are not assigned to any Resource will be included to the output as well
2222
* &showUnpublished int optional If 1 is set, Tags that are assigned only to unpublished Resources will be included to the output as well
2323
* &showDeleted int optional If 1 is set, Tags that are assigned only to deleted Resources will be included to the output as well
24+
* &linkCurrentTags int optional If 1 is set, Current Tags will be included in generated URL, default behavior is to generate links to a single tag
2425
* &contexts string optional If set, will display only tags for resources in given contexts. Contexts can be separated by a comma
2526
* &toPlaceholder string optional If set, output will return in placeholder with given name
2627
* &sort string optional Sort options in JSON. Example {"tag": "ASC"} or multiple sort options {"group_id": "ASC", "tag": "ASC"}
@@ -46,6 +47,7 @@
4647
$showUnused = (int) $modx->getOption('showUnused', $scriptProperties, '0');
4748
$showUnpublished = (int) $modx->getOption('showUnpublished', $scriptProperties, '0');
4849
$showDeleted = (int) $modx->getOption('showDeleted', $scriptProperties, '0');
50+
$linkCurrentTags = (int) $modx->getOption('linkCurrentTags', $scriptProperties, '0');
4951
$contexts = $modx->getOption('contexts', $scriptProperties, '');
5052
$translate = (int) $modx->getOption('translate', $scriptProperties, '0');
5153

@@ -172,26 +174,51 @@
172174

173175
$idx = 1;
174176
$currentTags = $tagger->getCurrentTags();
177+
$currentTagsLink = array();
178+
179+
if ($linkCurrentTags == 1) {
180+
foreach($currentTags as $currentTag)
181+
{
182+
$currentTagsLink[$currentTag['alias']] = array_keys($currentTag['tags']);
183+
}
184+
}
175185

176186
foreach ($tags as $tag) {
177187
/** @var TaggerTag $tag */
178188
$phs = $tag->toArray();
179189

180190
$group = $tag->Group;
181191

192+
$linkData = array_merge_recursive($currentTagsLink, array(
193+
$group->alias => array($tag->alias)
194+
));
195+
$linkData = array_filter(array_map(function($data) {
196+
return array_filter($data, function($value) use ($data) {
197+
return !(array_count_values($data)[$value] > 1);
198+
});
199+
}, $linkData));
200+
182201
if ($friendlyURL == 1) {
183-
$uri = rtrim($modx->makeUrl($target, '', '', $linkTagScheme), '/') . '/' . $group->alias . '/' . $tag->alias . '/';
202+
$linkPath = array_reduce(array_keys($linkData), function($carry, $item) use ($linkData) {
203+
return $carry . $item . '/' . implode('/', array_unique($linkData[$item])) . '/';
204+
}, '');
205+
$uri = rtrim($modx->makeUrl($target, '', '', $linkTagScheme), '/') . '/' . $linkPath;
184206
} else {
185-
$uri = $modx->makeUrl($target, '', $group->alias . '=' . $tag->alias, $linkTagScheme);
207+
$linkPath = http_build_query(
208+
array_map(function($values) {
209+
return is_array($values) ? implode(',', array_unique($values)) : $values;
210+
}, $linkData)
211+
);
212+
$uri = $modx->makeUrl($target, '', $linkPath, $linkTagScheme);
186213
}
187214

188215
$phs['uri'] = $uri;
189216
$phs['idx'] = $idx;
190217
$phs['target'] = $target;
191218
$phs['max_cnt'] = $maxCnt;
192-
219+
193220
if (isset($currentTags[$group->alias]['tags'][$tag->alias])) {
194-
$phs['active'] = 1;
221+
$phs['active'] = 1;
195222
} else {
196223
$phs['active'] = 0;
197224
}

0 commit comments

Comments
 (0)