Skip to content

Page variable is overwritten when page.js.twig is included in a dynamically loaded component #1104

@alexweissman

Description

@alexweissman

page.js.twig currently sets the page Javascript variable to import page-specific variables from the server into the client JS environment. However it looks like in some cases we load this partial both in the base page as well as dynamic components such as modal forms.

This means that the original value set in the base page gets completely overwritten when the modal form is loaded.

The simplest solution seems to be merging, rather than setting, the page variable by changing page.js.twig:

{# Page variables needed by client-side code (Javascript). #}
{% autoescape 'js' %}
    if (typeof page === 'undefined') {
      var page = {};
    }

    page = $.extend(
        true,               // deep extend
        {{ page | json_encode(constant('JSON_PRETTY_PRINT')) | raw }},
        page
    );
{% endautoescape %}

This doesn't completely guarantee that nested values in page won't be overwritten, of course. But it at least provides a mechanism for easily setting additional client-side variables while keeping them scoped under the top-level page object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed bugSomething isn't workingfrontendThe frontend interfaceneeds discussionA decision needs to be taken by the dev team

    Type

    No type

    Projects

    Status

    References

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions