Skip to content

[Issue] Magento_Backend store switcher: Use nowdoc instead of heredoc #32262

Closed
@m2-assistant

Description

@m2-assistant

This issue is automatically created based on existing pull request: #32249: Magento_Backend store switcher: Use nowdoc instead of heredoc

Description (*)

The heredoc in Magento/Backend/view/adminhtml/templates/store/switcher.phtml#L203-L216 is not valid, because the embedded JavaScript contains $this, which happens to be a valid variable name in PHP, too. Consequently, PHP tries to replace its occurrences by the current class object which results in an error. This can be avoided by using nowdoc instead of heredoc, which is the aim of this PR.

In Magento core, the is*using_iframe flag is never set. As a consequence, the else-branch in [Magento/Backend/view/adminhtml/templates/store/switcher.phtml#L203-L216](https://github.com/magento/magento2/blob/896f0d6/app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml#L203-L216] is never called, so the error does not occur in a standard environment. However, we use Amasty's [_Improved Layered Navigation*|https://amasty.com/improved-layered-navigation-for-magento-2.html), which sets this flag to true for one of their backend configurations. The resulting error (in developer mode) looks as follows:

Screenshot from 2021-02-23 08-32-05

Related Pull Requests

Two years ago, there was #20656 to replace all heredoc by nowdoc, but it was closed.

Fixed Issues (if relevant)

No separate issue created.

Preconditions (*)

Magento 2.4-develop

Steps to reproduce (*)

For reproducing this error, it is not necessary to install Magento or the referenced Amasty extension at all. Simply paste the snippet into a fresh PHP file test.php:

<?php
$test = <<<script
            jQuery('#preview*selected*store').val(scopeId);
            jQuery('#preview_form').submit();

            jQuery('.store-switcher .dropdown-menu li a').each(function() {
                var $this = jQuery(this);

                if ($this.data('role') === 'store-view-id' && $this.data('value') == scopeId) {
                    jQuery('#store-change-button').html($this.text());
                }
            });

            jQuery('#store-change-button').click();
script;
echo $test;
{code}

Calling the script via `php -f test.php` yields a similar error. Changing the first line to `<<<'script'` resolves this issue.

### Actual result
An Exception message appeared 
{code}
1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: test in .../app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml on line 227

Exception #0 (Exception): Notice: Undefined variable: test in .../app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml on line 227
<pre]#1 include() called at [lib/internal/Magento/Framework/View/TemplateEngine/Php.php:71]
#2 Magento\Framework\View\TemplateEngine\Php->render() called at [lib/internal/Magento/Framework/View/Element/Template.php:273]
#3 Magento\Framework\View\Element\Template->fetchView() called at [generated/code/Magento/Backend/Block/Store/Switcher/Interceptor.php:419]
#4 Magento\Backend\Block\Store\Switcher\Interceptor->fetchView() called at [lib/internal/Magento/Framework/View/Element/Template.php:303]
#5 Magento\Framework\View\Element\Template->_toHtml() called at [app/code/Magento/Backend/Block/Template.php:141]
...
#46 Magento\Backend\Model\View\Result\Page\Interceptor->renderResult() called at [lib/internal/Magento/Framework/App/Http.php:120]
#47 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:23]
#48 Magento\Framework\App\Http\Interceptor->launch() called at [lib/internal/Magento/Framework/App/Bootstrap.php:264]
#49 Magento\Framework\App\Bootstrap->run() called at [pub/index.php:29]
[/pre>

exc_before

Expected Result

No errors appear in the Admin panel. The Scope can be changed.

Metadata

Metadata

Assignees

Labels

Component: BackendIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: doneReported on 2.4.xIndicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S2Major restrictions or short-term circumventions are required until a fix is available.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions