MODX 3.2.1 modSymLink irritation

Hello,

I’m using symlink resources to output content from main resource.

I’m confused, that if I use $resource->get(‘class_key’) on the resource with class_key MODX\Revolution\modSymLink I get
MODX\Revolution\modDocument insteed.

so, I can’t decide to output correct canonical url for this resource.

What do I made wrong?

Thank you in advance for information:

Regards
Chris

Where do you get the variable $resource from?


When I run a test snippet, the code seems to return the correct class key MODX\Revolution\modSymLink for Symlinks:

<?php
$resourceId = 4; // resource ID of a Symlink resource
$r = $modx->getObject('MODX\Revolution\modResource', $resourceId);
if ($r) {
    return $r->get('class_key');
}

This seems to work as well:

$resourceId = 4; 
$r = $modx->getObject('MODX\Revolution\modResource', $resourceId);
if ($r) {
    if ($r instanceof \MODX\Revolution\modSymLink) {
        return "is a Symlink";
    } else {
        return "no Symlink";
    }
}

thank you for you answer.

$resource = $modx->resource;

if (!$resource) {
    return '';
}

$classKey = $resource->get('class_key');

// Standard: aktuelle Resource
$resourceId = (int) $resource->get('id');

// Bei Symlink: Zielresource aus "content" verwenden
if ($classKey === 'MODX\Revolution\modSymLink') {
    $resourceId = (int) $resource->get('content');
}

return $modx->makeUrl(
    $resourceId,
    '',
    '',
    'full'
);

know I corrected the snippet:

$resource = $modx->resource;

if (!$resource) {
return ‘’;
}

$resourceId = $modx->resource->get(‘id’);

$r = $modx->getObject(‘MODX\Revolution\modResource’, $resourceId);
if ($r) {
if ($r instanceof \MODX\Revolution\modSymLink) {
$resourceId = (int) $r->get(‘content’);
}
}

return $modx->makeUrl(
$resourceId,
‘’,
‘’,
‘full’
);

with this it runs..

thank you very much.

bye

Chris

With a symlink most field values (including by default the class_key) get overridden with the values from the target resource.

So if you want your original code to work, you have to change the system setting forward_merge_excludes (by removing class_key from the list), so that the original value for class_key is preserved.

thank you very much.

:folded_hands:

bye

Chris