MODX 2.8.4 - 404 OnPageNotFound Logic

Hello,

I think the logic for 404 pages “OnPageNotFound” is part of core logic.

If admin is logged in manager in front end pages that are not published are vieweable for admin.
How could I modify this logic oder extend it with my own?

I want show unpublished resources with special template for all.

I think I need a plugin with system even “OnPageNotFound”.
But how could I output the unpublished resource?

Thank you in advance.

bye
Chris

I’m not sure I understand exactly what you are trying to do.

I believe this is because of the permission view_unpublished.

The simplest way is probably to use sendForward to redirect to another published resource. Then on this resource use getResources/pdoResources to output the content of the requested page.

The only thing that would work with OnPageNotFound is to user $_SERVER['REQUEST_URI'] to get the request, then parse it for the alias, get the resource with that alias (if it exists and isn’t published), then figure out a way to display it’s fields (which you would already have). You could display them on the error page, or as halftrainedharry suggests, forward the user to another page where they would be displayed.

thank’s a lot.

I did it exactly so.

<?php
/**
 * ShowUnpublishedAd
 * 
 * @var modX $modx
 */
if ($modx->event->name != 'OnPageNotFound') return;

/* handle redirects */
$search = $_SERVER['REQUEST_URI'];
$base_url = $modx->getOption('base_url');
if ($base_url != '/') {
    $search = str_replace($base_url,'',$search);
}
$search = trim($search, '/');


/* get resource to redirect to */
$resourceId = false;

if(is_array($modx->aliasMap)) {
    
    $resourceId = $modx->aliasMap[$search];
    
    if ( $resourceId <> '' ){
        $ad = $modx->getObject('modResource', array('id' => $resourceId, 'publishedby:!=' => ''));
        $template = $ad->get('template');
        
        if( $template == 2 ){
            $modx->setPlaceholder('id',  $resourceId);
            $modx->sendForward(153);
        }else
            return;
            
    }
    
}

return;

Nice! I especially like the use of the aliasmap and sendforward().