Invision Community 4: SEO, prepare for v5 and dormant account notifications Matt November 11, 2024Nov 11
Posted April 4Apr 4 Hello all! I'm building my own language switcher because i'd like my switcher to be added the mobile user menu. I have got it working, it switches to the language that I click on. But now I have ran into the issue that when you are on a page like "/contact/" you'd still be redirected to the homepage.I have tried a lot and I can't seem to get it working. Can anybody with more experience point me into the right direction?Much appreciated, thanks!/** * @brief UserMenu extension: languageSwitcher */ class languageSwitcher extends MenuExtension { /** * Modify the account menu by adding language switcher items. * * @param string $position Position of the menu (content, settings, logout) * @return array<MenuItem> */ public function mobileMenu( string $position = 'content' ): array { $menuItems = []; if ($position === 'content') { $currentUrl = \IPS\Request::i()->url(); // Get the current page URL // English language switch link $englishUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=1', 'front') ->csrf(); // Add CSRF token $englishLink = new \IPS\Helpers\Menu\Link( $englishUrl, 'English', 'ipsMenu_item' ); $menuItems[] = $englishLink; // Dutch language switch link $dutchUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=2', 'front') ->csrf(); // Add CSRF token $dutchLink = new \IPS\Helpers\Menu\Link( $dutchUrl, 'Dutch', 'ipsMenu_item' ); $menuItems[] = $dutchLink; } return $menuItems; }
April 4Apr 4 Solution $dutchUrl = \IPS\Http\Url::internal('app=core&module=system&controller=language&id=2', 'front') ->csrf() // Add CSRF token ->setQueryString( 'ref', base64_encode( $currentUrl ) ); // set ref urlshould do it.You can always add the 'ref' parameter to an url which contains the base64 encoded target URL.
April 4Apr 4 Much appreciated, thanks!So you added this to your code: $currentUrl = \IPS\Request::i()->url(); // Get the current page URLAnd never used anymore? You must find that code is useful but didn't tried to explore further? :)
Monday at 10:04 AM4 days Author Thanks @Daniel F, this got it working :).@DawPi At first I had something like the code below there, but this didn't work so I removed it, but forgot the remove $currentUrl. That's why haha.->setQueryString( 'return', base64_encode( $currentUrl ) );