getgrav / grav-plugin-langswitcher

Grav LangSwitcher Plugin
https://getgrav.org
MIT License
28 stars 26 forks source link

Non-existent page: Call to a member function slug() on null #66

Closed zegkljan closed 2 years ago

zegkljan commented 2 years ago

When I go to a page that does not exist, instead of 404 page, I'm greeted with an ugly error caused by langswitcher:

Error thrown with message "Call to a member function slug() on null"

Stacktrace:
#48 Error in /var/www/html/user/plugins/langswitcher/langswitcher.php:80
#47 Grav\Plugin\LangSwitcherPlugin:_getTranslatedUrl in /var/www/html/user/plugins/langswitcher/langswitcher.php:143
#46 Grav\Plugin\LangSwitcherPlugin:onTwigSiteVariables in /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:264
#45 Symfony\Component\EventDispatcher\EventDispatcher:doDispatch in /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:239
#44 Symfony\Component\EventDispatcher\EventDispatcher:callListeners in /var/www/html/vendor/symfony/event-dispatcher/EventDispatcher.php:73
#43 Symfony\Component\EventDispatcher\EventDispatcher:dispatch in /var/www/html/system/src/Grav/Common/Grav.php:592
#42 Grav\Common\Grav:fireEvent in /var/www/html/system/src/Grav/Common/Twig/Twig.php:413
#41 Grav\Common\Twig\Twig:processSite in /var/www/html/system/src/Grav/Common/Service/OutputServiceProvider.php:36
#40 Grav\Common\Service\OutputServiceProvider:Grav\Common\Service\{closure} in /var/www/html/vendor/pimple/pimple/src/Pimple/Container.php:122
#39 Pimple\Container:offsetGet in /var/www/html/system/src/Grav/Common/Processors/RenderProcessor.php:40
#38 Grav\Common\Processors\RenderProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#37 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#36 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/DebuggerAssetsProcessor.php:38
#35 Grav\Common\Processors\DebuggerAssetsProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#34 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#33 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/PagesProcessor.php:112
#32 Grav\Common\Processors\PagesProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#31 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#30 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/TwigProcessor.php:38
#29 Grav\Common\Processors\TwigProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#28 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#27 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/AssetsProcessor.php:39
#26 Grav\Common\Processors\AssetsProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#25 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#24 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/SchedulerProcessor.php:40
#23 Grav\Common\Processors\SchedulerProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#22 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#21 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/BackupsProcessor.php:39
#20 Grav\Common\Processors\BackupsProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#19 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#18 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/TasksProcessor.php:69
#17 Grav\Common\Processors\TasksProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#16 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#15 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/RequestProcessor.php:64
#14 Grav\Common\Processors\RequestProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#13 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#12 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/ThemesProcessor.php:38
#11 Grav\Common\Processors\ThemesProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#10 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#9 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/PluginsProcessor.php:39
#8 Grav\Common\Processors\PluginsProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#7 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#6 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Processors/InitializeProcessor.php:130
#5 Grav\Common\Processors\InitializeProcessor:Grav\Common\Processors\{closure} in /var/www/html/system/src/Grav/Common/Debugger.php:546
#4 Grav\Common\Debugger:profile in /var/www/html/system/src/Grav/Common/Processors/InitializeProcessor.php:131
#3 Grav\Common\Processors\InitializeProcessor:process in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:50
#2 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php:62
#1 Grav\Framework\RequestHandler\RequestHandler:handle in /var/www/html/system/src/Grav/Common/Grav.php:307
#0 Grav\Common\Grav:process in /var/www/html/index.php:47
rhukster commented 2 years ago

i’ll look at this today.

maofree commented 2 years ago

Hi in langswitcher.php to fix it is necessary to add a condition to the while cycle pf the function _getTranslatedUrl

while ($max_recursions > 0 && $current_node !== null && $current_node->slug() != 'pages' && $path != 'pages') {

$current_node !== null

I got the same problem and I fix it in this mode so the page return a 404 error in the right mode

1

rhukster commented 2 years ago

Good call.. i've used your solution