Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.2.0-rc3.0] Language switcher is broken when using multiple times #10908

Closed
hostep opened this issue Sep 16, 2017 · 14 comments
Closed

[2.2.0-rc3.0] Language switcher is broken when using multiple times #10908

hostep opened this issue Sep 16, 2017 · 14 comments
Labels
Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release

Comments

@hostep
Copy link
Contributor

hostep commented Sep 16, 2017

Preconditions

  1. PHP 7.0.22

Steps to reproduce

  1. Install Magento CE 2.2.0-rc3.0 using composer
  2. Switch to developer mode
  3. In the backend, make sure you have 2 storeviews (in my example: Dutch & English)
  4. Reindex & flush caches
  5. Load the frontend
  6. Using the language switcher in the header, switch from the default storeview (Dutch in my example) to English, the url changes to: https://example.com/?___store=en
  7. Using the language switcher in the header, switch again from English to Dutch, the url changes to: https://example.com/?___store=en?___store=nl and the shop crashes

Expected result

  1. The url should be https://example.com/?___store=nl and the shop shouldn't crash

Actual result

  1. The url is https://example.com/?___store=en?___store=nl and the shop crashes with the message:
1 exception(s):
Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Requested store is not found

Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Requested store is not found
#0 vendor/magento/module-store/Model/StoreManager.php(168): Magento\Store\Model\StoreRepository->get('en?___store=nl')
#1 vendor/magento/module-store/App/Action/Plugin/Context.php(85): Magento\Store\Model\StoreManager->getStore('en?___store=nl')
#2 vendor/magento/framework/Interception/Interceptor.php(121): Magento\Store\App\Action\Plugin\Context->beforeDispatch(Object(Magento\Cms\Controller\Index\Index\Interceptor), Object(Magento\Framework\App\Request\Http))
#3 vendor/magento/framework/Interception/Interceptor.php(153): Magento\Cms\Controller\Index\Index\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#4 generated/code/Magento/Cms/Controller/Index/Index/Interceptor.php(39): Magento\Cms\Controller\Index\Index\Interceptor->___callPlugins('dispatch', Array, Array)
#5 vendor/magento/framework/App/FrontController.php(55): Magento\Cms\Controller\Index\Index\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#6 vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#7 vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#8 vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#9 vendor/magento/framework/Interception/Interceptor.php(135): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#10 vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(73): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#11 vendor/magento/framework/Interception/Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#12 vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#13 generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, NULL)
#14 vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#15 vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http->launch()
#16 pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#17 {main}

Discussion

This was only very recently broken, in commit: c3ea1f5, the changes in the file lib/internal/Magento/Framework/App/Request/Http.php cause this.

Also: please change this typo from removeRepitedSlashes to removeRepeatedSlashes, so this method has the same name as in Magento 2.1

Possible solution

If we change this line from:

$requestString = $this->_url->escape(ltrim($this->_request->getRequestString(), '/'));

to:

$requestString = $this->_url->escape(ltrim($this->_request->getOriginalPathInfo(), '/'));

It works again as expected, but I'm not sure yet if this is the correct solution.

@magento-engcom-team magento-engcom-team added Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed labels Sep 16, 2017
@magento-engcom-team
Copy link
Contributor

@hostep thank you for your bug report.
We've created internal ticket MAGETWO-75448 to track progress on the issue.

@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Sep 19, 2017
@vrootwoot
Copy link

Apparently the issue only occurs from the homepage, starting working on it.

@BlackIkeEagle
Copy link
Member

@vrootwoot you still working on this? I have this issue too while I'm working on some other issue in the store switcher

@vrootwoot
Copy link

@BlackIkeEagle won't have the time to fix it.

Apparently correct way would be to add changes to getTargetStorePostData
in app/code/Magento/Store/Block/Switcher.php.
And also change the entry points in the Switcher Block as well.

@LuisChaves
Copy link

In addition to changing the line of code suggested by hostep, to also remove "?___ store". Change line 228 "$store->getCurrentUrl(false)," to "$this->getUrl ('stores/store/switch' ), "from the file" module-store \ Block \ Switcher.php "

@selusi
Copy link

selusi commented Oct 13, 2017

However, the bug is much more complex, because if you change the language on page of the product details or on a category page the app give a not found page, because does not translate the urlrewrite.
In the previous version Magento 2.1.9 the switcher worked fine.

@selusi
Copy link

selusi commented Oct 14, 2017

The @LuisChaves solution solves the problem definitively, but at this point the getCurrentUrl method in the Store class is absolutely useless.

I've deleted the getCurrentUrl () method from Magento \ Store \ Model \ Store and modified line 228 of the getTargetStorePostData function in Magento \ Store \ Block \ Switcher as follows:

$this->getUrl('stores/store/switch',  ['_secure' => $this->_storeManager->getStore()->isCurrentlySecure()]),

Finally everything works well.

NOTE: I would like to point out another bug in the store module, which I found a solution to the following link:
Magento 2.2 fix Home Page error if store view is not defined #11453

@hostep
Copy link
Contributor Author

hostep commented Oct 14, 2017

@selusi: just FYI, you've changed the code back to how it was before commit 508f1ef, in this commit, they fixed the storeswitcher when you work with multiple domain names since it used to store the wrong store code in a cookie which has as a result that we ended up in an endless redirect from one domain name to the other.
It might be worth to test if it still works when switching stores with multiple domain names if you revert that line back to $this->getUrl('stores/store/switch'), but I have the feeling that the original problem will be back if you do this.

(unrelated, but this page might be worth checking out for formatting your comments on github a bit better: https://guides.github.com/features/mastering-markdown/, please check the 'code' tab in the Examples 😉)

@selusi
Copy link

selusi commented Oct 14, 2017

@hostep Thank you for the clarification. I apologize, because I did not read the commit 508f1ef you rightly mentioned.
So everything is again in question, because your code solve only a part of issue.

@thiagolima-bm
Copy link
Member

@okobchenko PR #11337

@okorshenko
Copy link
Contributor

the issue has been fixed and delivered to 2.2-develop branch. WIll be available with 2.2.2 release

@magento-engcom-team
Copy link
Contributor

Hi @hostep. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1285 by @magento-engcom-team in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming patch release.

@mariamghalleb
Copy link

@magento-engcom-team the link doesn't work ..

@MrDaar
Copy link

MrDaar commented Apr 25, 2018

It's not good code, and it's not thoroughly tested, but here's a temp fix if you're currently experiencing this issue:

<type name="Magento\Store\Block\Switcher">
    <plugin name="Vendor_Module_Plugin_Magento_Store_Block_Switcher"
            type="Vendor\Module\Plugin\Magento\Store\Block\Switcher"
            sortOrder="0"
    />
</type>
<?php
namespace Vendor\Module\Plugin\Magento\Store\Block;

class Switcher
{
    /**
     * Returns target store post data
     *
     * @see https://github.com/magento/magento2/issues/10908
     * @param \Magento\Store\Block\Switcher $subject
     * @param string $postData
     * @return string
     */
    public function afterGetTargetStorePostData(
        \Magento\Store\Block\Switcher $subject,
        $postData
    ) {
        $data = json_decode($postData, true);

        if (empty($data['action'])) {
            return $postData;
        }

        if (substr_count($data['action'], '___store') < 2) {
            return $postData;
        }

        $data['action'] = preg_replace('/___store=(.*?)___store/', '___store', $data['action']);

        return json_encode($data);
    }
}

Let me know if it works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release
Projects
None yet
Development

No branches or pull requests