We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 630
    • 39 Posts
    Sometimes I want to put a link on a published page, but the link refers to a page not yet published, or that page is kind of unstable in regard to its publishedness, or not all webusers are authorised to access that page, just: a page that may turn out unavailable to visitors under certain circumstances.

    Unfortunately, when I use the common notation [~id~], modx renders a link no matter if the visitor might get annoyed by an error page coming up and saying that he/she cannot access the requested page. Isn’t that one of the purposes why we use CMS: Tracking the availability of internal links? (no criticism, I just wonder)

    So, I wrote the following snippet as I didn’t find an existing appropriate one in the repo (but searched only the "navigation"-tagged resources).
    It is optimized, i.e. primarily intended for semantically explicit in-line links. For links of other kind adjustment is possible by additional parameters.

    <?php
    
    /* Sensitive Link
     * description: Renders a link only if the web visitor can access the page id to which it refers
     * version: 0.1
     * author: Florian 'agilero' L. Hess
     * tested MODx versions: 0.9.6.3
     * license: GPL
     *
     * Installation:
     *    - Call Resources > Manage Resources > Snippets > New Snippet routine in the manager,
     *    - give it a preferably short name, e.g. 'link',
     *    - copy version and description above,
     *    - and the whole code into the snippet code field
     *
     * Usage/Parameters:
     *    Call: [[link?
     *      &id=`docID` # MANDATORY: modx document identifier
     *      &text=`link text, <img>, whatever` # the content of the anchor element
     *      &class=`class(-es, space-delimited)` # class or chain of class names to style the link (OPTIONAL)
     *      &title=`template variable name` # use content of this document variable for title attribute (default: pagetitle)
     *      &posClass=`class(-es, sp.-del.)` # further classes to apply only when page is accessible (OPTIONAL)
     *      &negClass=`class(-es, sp.-del.)` # further classes to apply otherwise (OPTIONAL)
     *      &negTitle=`string` # content of title attribute if the link is not accessible (OPTIONAL)
     *     ]]
     *
     * NOTE: The link is rendered as anchor (<a>...</a>) whether or not accessible, but without href attribute if not.
     * An anchor missing both 'href' and 'name' attribute does not hurt validity of the html. But what is the behaviour
     * of non-Mozilla browsers, do they render a void anchor wrongly in a way distinct from the surrounding text?
     *
     * TODO: Membership should also be respected for protected or private pages, but how? Digged the docs, yet no idea
     *
     */ 
      
    $title = isset($title) ? $title : 'pagetitle';
    
    $posClass = isset($posClass) ? ($class . ' ' . $posClass) : $class;
    $negClass = isset($negClass) ? ($class . ' ' . $negClass) : $class;
    
    $pi = $modx->getPageInfo($id,1,$title);
    
    if ( $pi ) {
        $pre = sprintf( '<a href="%s" class="%s" title="%s">', $modx->makeurl($id, $alias, $args, $scheme), $posClass, $title );
    }
    
    else {
        $pre = "<a class=\"$negClass\" " . (isset($negTitle) ? 'title="'.$negTitle.'"' : '') . ">";
    }
    
    return $pre.$text.'</a>';
    
    ?>


    Feedback welcome smiley

      • 630
      • 39 Posts
      Ah, quite surprised that I haven’t got feedback since. Is that snippet not useful? Well, okay *shrug* smiley

      Ooops, by the way, I forgot to mention the most important parameter &text=`link text, <img>, whatever`. Might that be why you couldn’t proove the usefulness of this snippet? Sorry.