How to avoid bad link tag errors in the error log

Using [[~[[+tv.pageLink]]]] throws lots of errors in the error log. (bad link tag)
The tv.pageLink is not set most of the times, which causes the errors.

So I try

[[+tv.pageLink:notempty=`[[~[[tv.pageLink]]]]`]]

but that doesn’t help since the inner part is being parsed nonetheless.

Isn’t there a ‘standard’ way available in ModX to do this without it clogging the error-log?

Maybe this works:

[[ [[+tv.pageLink:notempty=`~[[+tv.pageLink]]`]] ]]

But as this is a bit ridiculous, it’s probably a better idea to create a custom snippet that uses $modx->makeUrl() when the TV is not empty.

1 Like

This output filter can help:

<?php
/**
 * LinkThis
 * Generate a link from an integer, that references a resource or a weblink (and its content value). Otherwise, use the value as an url. If the link is empty try the same with an optional default value.
 *
 * @author Thomas Jakobi <office@treehillstudio.com>
 *
 * @usage [[+placeholder:linkthis=`default`]]
 */

/** @var modX $modx */
/** @var array $scriptProperties */

$value = $modx->getOption('input', $scriptProperties, '');
$default = $modx->getOption('options', $scriptProperties, '');

if (!function_exists('linkthis')) {
    function linkthis($modx, $value)
    {
        if (filter_var($value, FILTER_VALIDATE_INT) !== false) {
            $resource = $modx->getObject('modResource', $value);
            if ($resource) {
                if ($resource->get('content_type') === 'modWebLink' || $resource->get('content_type') === 'MODX\Revolution\modWebLink') {
                    return ($resource->get('content')) ? $modx->makeUrl($resource->get('content')) : '';
                } else {
                    return $modx->makeUrl($value);
                }
            } elseif ($value) {
                $modx->log(xPDO::LOG_LEVEL_ERROR, 'The resource ' . $value . ' does not exist!');
                return $value;
            } else {
                return '';
            }
        } elseif ($value) {
            return $value;
        } else {
            return '';
        }
    }
}

$result = linkthis($modx, $value);
if (!$result) {
    $result = linkthis($modx, $default);
}

return $result;
1 Like

I’m not sure why it is ridiculous. What would be a valid use case for [[ ~[[+tv.pageLink]] ]] or [[ ~[[+id]] ]] then?

I’ll keep this in mind as the bad link tag error will come back at me :slight_smile:

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.