2

I discover that some native route are listed in the section xml file without any section :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="customer/account/logout"/>
    <action name="customer/account/loginPost"/>
    <action name="customer/account/createPost"/>
    <action name="customer/account/editPost"/>
    <action name="customer/ajax/login">
        <section name="checkout-data"/>
        <section name="cart"/>
    </action>
</config>

In this case the SectionConfigConverter.php will add '*' to signify all sections need to be reloaded. But when one of this route are used, magento try to reload this sections and I got :

"*" section source is not supported

It doesn't seem that the native code is able to deal with the '*' it has added itself. Have you already encounter/resolve this bug ?

Thank you in advance

2
  • Could you tell more about how to reproduce that error? Jan 4, 2019 at 10:11
  • You can reproduce this bug by calling jQuery.post('/index.php/customer/account/createPost/', []) in your web browser console (I known this call is useless, but it's the easiest and fastest way to reporduce the bug i found) Jan 4, 2019 at 11:02

1 Answer 1

4

I can't understand why this is not working out of the box, but here is workaround for issue:

Plugin which makes this job. In case the * symbol was found in list of section names it makes the section name equals null which means for the Magento\Customer\CustomerData\SectionPool class to update all the sections.

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Plugins -->
    <type name="Magento\Customer\CustomerData\SectionPool">
        <plugin name="workaround_update_all_sections"
                type="MageWorx\CustomerDataFix\Plugin\UpdateAllCustomerDataSections"
                sortOrder="10"
                disabled="false"/>
    </type>
</config>

app/code/MageWorx/CustomerDataFix/Plugin/UpdateAllCustomerDataSections.php

<?php
/**
 * Copyright © MageWorx. All rights reserved.
 * See LICENSE.txt for license details.
 */

namespace MageWorx\CustomerDataFix\Plugin;

use Magento\Customer\CustomerData\SectionPool;

/**
 * Class UpdateAllCustomerDataSections
 *
 * Update all sections in case the wildcard symbol was found in the sections-to-update array
 */
class UpdateAllCustomerDataSections
{
    /**
     * @param SectionPool $subject
     * @param array|null $sectionNames
     * @param bool $updateIds
     * @return array
     */
    public function beforeGetSectionsData(
        SectionPool $subject,
        array $sectionNames = null,
        $updateIds = false
    ) {
        // Trying to find a wildcard in the "sections-to-update" array
        if (!empty($sectionNames) && array_search('*', $sectionNames) !== false) {
            // If found drop all section names to allow update of all sections
            $sectionNames = null;
        }

        return [$sectionNames, $updateIds];
    }
}

Complete code (module) could be found here on GitHub.

I found a strange code and I think that it should be responsible for this:

Magento_Customer/js/section-config.js

    getAffectedSections: function (url) {
        var route = canonize(url),
            actions = _.find(sections, function (val, section) {
                var matched;

                if (section.indexOf('*') >= 0) {
                    section = section.replace(/\*/g, '[^/]+') + '$';
                    matched = route.match(section);

                    return matched && matched[0] == route; //eslint-disable-line eqeqeq
                }

                return route.indexOf(section) === 0;
            });

        return _.union(_.toArray(actions), _.toArray(sections['*']));
    },

but a route could match the * section only in case it has no /. I don't know how it could be achieved in real world :\ Maybe it could be useful for you somehow.

2
  • Thank you for your response, I think your plugin will resolve the bug, but I found this strange that magento can't deal with this case natively. I find a way around by explicitly liste the sections for these routes in my sections.xml file. About the strange code you found in js, I think it is used for global url such as customer/account/* Jan 4, 2019 at 13:39
  • @PierreGauthier This plugin does not prevent loading in case * was not found in the list of section names. In case the * exist in the section names array it just reload all the sections: you can see the \Magento\Customer\CustomerData\SectionPool::getSectionsData() method where code, which is responsible for update sections, looks like $sectionsData = $sectionNames ? $this->getSectionDataByNames($sectionNames) : $this->getAllSectionData();. So when the $sectionNames === null all sections will be reloaded. Jan 4, 2019 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.