Fuzzy Posted May 28, 2018 Posted May 28, 2018 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?
Sergio Posted May 28, 2018 Posted May 28, 2018 You may need a hook to get it done, take a look on this thread: 1
kixe Posted May 28, 2018 Posted May 28, 2018 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); } }); 3
Fuzzy Posted May 30, 2018 Author Posted May 30, 2018 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?
kixe Posted May 30, 2018 Posted May 30, 2018 @Fuzzy Place this code in /site/ready.php Create this file if it doesn't exist.
Fuzzy Posted June 6, 2018 Author Posted June 6, 2018 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... 1
kixe Posted June 6, 2018 Posted June 6, 2018 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?
Fuzzy Posted June 6, 2018 Author Posted June 6, 2018 ProcessWire 2.7.3 and code is placed in file ready.php which is in folder "site". I've only changes the page id...
kixe Posted June 6, 2018 Posted June 6, 2018 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)
Fuzzy Posted June 6, 2018 Author Posted June 6, 2018 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?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now