Jump to content

Display no 404 page when page inactive


Recommended Posts

Hi there,

Can't find a solution for this problem (although it sounds quite easy): If a page exists but is not active in the current user's language there should be an alert with "content not available for your selected region" instead of displaying the 404-page.

I tried this:

// if page is not active in current user language show alert, instead of 404 page
if($page->name != 'http404' && !$page->viewable($user->language)) {
    $page = $pages->get('id=17104'); // this is the page containing the content-not-available-in-your-region-message
}

It works when logged in as superuser but when loged out, visitors will always be redirected to the 404-page.

Can anyone help, please?

Link to post
Share on other sites

Try this hook in hook

$this->addHookBefore('PageRender::renderPage', function($e) {
    $event = $e->arguments[0];
    $page = $event->object;
    $user = $this->wire('user');

    // QUICK EXIT
    if ($user->isSuperuser()) return; // superuser
    if (!empty($page->template) && $page->template->id == 2) return; // backend
    if ($user->language->isDefault()) return; // default language

    $language = $user->language;
    $currentLanguageStatus = $page->get("status$language");
    if ($currentLanguageStatus == 0) {
        $otherPage = $this->wire('pages')->get(1234); // get page to be rendered instead
        $event = new HookEvent(array('object' => $otherPage));
        $e->arguments(0, $event);
    }
});

 

  • Like 3
Link to post
Share on other sites

Thanks @kixe and @Sergio for trying to help me. I've added @kixe's code at the top of my prepend.inc (and changed the page id) but the 404 is still displayed. I also tried ProcessPageView::pageNotFound instead of PageRender::renderPage - but without success!

Any ideas?

 

Link to post
Share on other sites

Hi @kixe,

Sorry to bother you again. Could you please check your code again? I get the following error

Error: Class 'ProcessWire\HookEvent' not found (for this line $event = new HookEvent(array('object' => $otherPage)); )

PS: Sorry, I'm still learning and have to admit that hooks are out of my range...

  • Like 1
Link to post
Share on other sites

I tested my code and it works. Maybe there is a namespace issue. Which PW Version do you use? Did you place the code in the right file and folder?

Link to post
Share on other sites

Namespace problem. Somewhere in your files (ready.php?) the namespace ProcessWire is defined. You need to remove this. Don't mix files of installations (PW 3.0.x) using namespace with non namespace installations (PW 2.7 or 2.8)

Link to post
Share on other sites

Still not getting it. Removing namespace Processwire from ready.php solved the error but the 404 is still being displayed. After some investigations included the following in config.php - but no effect...

$config->templateCompile = false;
$config->moduleCompile = false;

Any ideas?

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...