SonataFormatterBundle icon indicating copy to clipboard operation
SonataFormatterBundle copied to clipboard

SonataFormatterBundle + CKEditor don't work with the new AssetMapper

Open lukepass opened this issue 3 months ago • 3 comments

Environment

Symfony 6.4 Ubuntu OS

Sonata packages

show

$ composer show --latest 'sonata-project/*'
Color legend:
- patch or minor release available - update recommended
- major release available - update possible
- up to date version
sonata-project/admin-bundle              4.29.3 4.29.3 The missing Symfony Admin Generator
sonata-project/block-bundle              5.1.0  5.1.0  Symfony SonataBlockBundle
sonata-project/doctrine-extensions       2.4.0  2.4.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 4.16.0 4.16.0 Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  3.3.0  3.3.0  Lightweight Exporter library
sonata-project/form-extensions           2.3.0  2.3.0  Symfony form extensions
sonata-project/formatter-bundle          5.4.1  5.4.1  Symfony SonataFormatterBundle
sonata-project/media-bundle              4.11.1 4.12.0 Symfony SonataMediaBundle
sonata-project/translation-bundle        3.3.0  3.3.0  SonataTranslationBundle
sonata-project/twig-extensions           2.4.0  2.4.0  Sonata twig extensions

Symfony packages

show

$ composer show --latest 'symfony/*'
Color legend:
- patch or minor release available - update recommended
- major release available - update possible
- up to date version
symfony/apache-pack                v1.0.1  v1.0.1  A pack for Apache support in Symfony
symfony/asset                      v6.4.3  v7.0.3  Manages URL generation and versioning of web assets such as CSS style...
symfony/asset-mapper               v6.4.5  v7.0.5  Maps directories of assets & makes them available in a public directo...
symfony/browser-kit                v6.4.3  v7.0.3  Simulates the behavior of a web browser, allowing you to make request...
symfony/cache                      v6.4.4  v7.0.4  Provides extended PSR-6, PSR-16 (and tags) implementations
symfony/cache-contracts            v3.4.0  v3.4.0  Generic abstractions related to caching
symfony/clock                      v6.4.5  v7.0.5  Decouples applications from the system clock
symfony/config                     v6.4.4  v7.0.4  Helps you find, load, combine, autofill and validate configuration va...
symfony/console                    v6.4.4  v7.0.4  Eases the creation of beautiful and testable command line interfaces
symfony/css-selector               v6.4.3  v7.0.3  Converts CSS selectors to XPath expressions
symfony/debug-bundle               v6.4.3  v7.0.3  Provides a tight integration of the Symfony VarDumper component and t...
symfony/dependency-injection       v6.4.4  v7.0.4  Allows you to standardize and centralize the way objects are construc...
symfony/deprecation-contracts      v3.4.0  v3.4.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v6.4.5  v7.0.5  Provides integration for Doctrine with various Symfony components
symfony/doctrine-messenger         v6.4.4  v7.0.4  Symfony Doctrine Messenger Bridge
symfony/dom-crawler                v6.4.4  v7.0.4  Eases DOM navigation for HTML and XML documents
symfony/dotenv                     v6.4.4  v7.0.4  Registers environment variables from a .env file
symfony/error-handler              v6.4.4  v7.0.4  Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           v6.4.3  v7.0.3  Provides tools that allow your application components to communicate ...
symfony/event-dispatcher-contracts v3.4.0  v3.4.0  Generic abstractions related to dispatching event
symfony/expression-language        v6.4.3  v7.0.3  Provides an engine that can compile and evaluate expressions
symfony/filesystem                 v6.4.3  v7.0.3  Provides basic utilities for the filesystem
symfony/finder                     v6.4.0  v7.0.0  Finds files and directories via an intuitive fluent interface
symfony/flex                       v2.4.5  v2.4.5  Composer plugin for Symfony
symfony/form                       v6.4.4  v7.0.4  Allows to easily create, process and reuse HTML forms
symfony/framework-bundle           v6.4.4  v7.0.4  Provides a tight integration between Symfony components and the Symfo...
symfony/google-mailer              v6.4.3  v7.0.3  Symfony Google Mailer Bridge
symfony/http-client                v6.4.5  v7.0.5  Provides powerful methods to fetch HTTP resources synchronously or as...
symfony/http-client-contracts      v3.4.0  v3.4.0  Generic abstractions related to HTTP clients
symfony/http-foundation            v6.4.4  v7.0.4  Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                v6.4.5  v7.0.5  Provides a structured process for converting a Request into a Response
symfony/intl                       v6.4.3  v7.0.3  Provides access to the localization data of the ICU library
symfony/mailer                     v6.4.4  v7.0.4  Helps sending emails
symfony/maker-bundle               v1.56.0 v1.56.0 Symfony Maker helps you create empty commands, controllers, form clas...
symfony/messenger                  v6.4.4  v7.0.4  Helps applications send and receive messages to/from other applicatio...
symfony/mime                       v6.4.3  v7.0.3  Allows manipulating MIME messages
symfony/monolog-bridge             v6.4.4  v7.0.3  Provides integration for Monolog with various Symfony components
symfony/monolog-bundle             v3.10.0 v3.10.0 Symfony MonologBundle
symfony/notifier                   v6.4.3  v7.0.3  Sends notifications via one or more channels (email, SMS, ...)
symfony/options-resolver           v6.4.0  v7.0.0  Provides an improved replacement for the array_replace PHP function
symfony/password-hasher            v6.4.4  v7.0.4  Provides password hashing utilities
symfony/phpunit-bridge             v7.0.4  v7.0.4  Provides utilities for PHPUnit, especially user deprecation notices m...
symfony/polyfill-intl-grapheme     v1.29.0 v1.29.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.29.0 v1.29.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.29.0 v1.29.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.29.0 v1.29.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.29.0 v1.29.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php83             v1.29.0 v1.29.0 Symfony polyfill backporting some PHP 8.3+ features to lower PHP vers...
symfony/process                    v6.4.4  v7.0.4  Executes commands in sub-processes
symfony/property-access            v6.4.4  v7.0.4  Provides functions to read and write from/to an object or array using...
symfony/property-info              v6.4.3  v7.0.3  Extracts information about PHP class' properties using metadata of po...
symfony/routing                    v6.4.5  v7.0.5  Maps an HTTP request to a set of configuration variables
symfony/runtime                    v6.4.3  v7.0.3  Enables decoupling PHP applications from global state
symfony/security-acl               v3.3.3  v3.3.3  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v6.4.5  v7.0.5  Provides a tight integration of the Security component into the Symfo...
symfony/security-core              v6.4.3  v7.0.3  Symfony Security Component - Core Library
symfony/security-csrf              v6.4.3  v7.0.3  Symfony Security Component - CSRF Library
symfony/security-http              v6.4.4  v7.0.4  Symfony Security Component - HTTP Integration
symfony/serializer                 v6.4.4  v7.0.4  Handles serializing and deserializing data structures, including obje...
symfony/service-contracts          v3.4.1  v3.4.1  Generic abstractions related to writing services
symfony/stimulus-bundle            v2.16.0 v2.16.0 Integration with your Symfony app & Stimulus!
symfony/stopwatch                  v6.4.3  v7.0.3  Provides a way to profile code
symfony/string                     v6.4.4  v7.0.4  Provides an object-oriented API to strings and deals with bytes, UTF-...
symfony/translation                v6.4.4  v7.0.4  Provides tools to internationalize your application
symfony/translation-contracts      v3.4.1  v3.4.1  Generic abstractions related to translation
symfony/twig-bridge                v6.4.4  v7.0.4  Provides integration for Twig with various Symfony components
symfony/twig-bundle                v6.4.4  v7.0.4  Provides a tight integration of Twig into the Symfony full-stack fram...
symfony/ux-turbo                   v2.16.0 v2.16.0 Hotwire Turbo integration for Symfony
symfony/validator                  v6.4.4  v7.0.5  Provides tools to validate values
symfony/var-dumper                 v6.4.4  v7.0.4  Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               v6.4.4  v7.0.4  Allows exporting any serializable PHP data structure to plain PHP code
symfony/web-link                   v6.4.3  v7.0.3  Manages links between resources
symfony/web-profiler-bundle        v6.4.4  v7.0.4  Provides a development tool that gives detailed information about the...
symfony/yaml                       v6.4.3  v7.0.3  Loads and dumps YAML files

PHP version

$ php -v
PHP 8.2.15 (cli) (built: Jan 20 2024 14:16:39) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.15, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.15, Copyright (c), by Zend Technologies

Subject

I tried to make SonataFormatter work with a new Symfony 6.4 project and AssetMapper but when going to production the files can't be found by the new AssetMapper component and the result is a 404 Not Found.

If I run sf debug:asset-map --full in production, the CKEditor files are not in the list.

In the development environment the CKEditor is displayed correctly and works but when going to production, after running asset-map:compile the files cannot be found.

I tried a lot of things, including "forcing" the AssetMapper to find the assets like this:

asset_mapper.yaml:

framework:
    asset_mapper:
        # The paths to make available to the asset mapper.
        paths:
            assets/: ''
            vendor/friendsofsymfony/ckeditor-bundle/src/Resources/public/: 'bundles/fosckeditor'

I also tried to override the base layout to add the CKEDITOR_BASEPATH constant:

{# the special '!' prefix avoids errors when extending from an overridden template #}
{% extends '@!SonataAdmin/standard_layout.html.twig' %}

{% block sonata_javascript_config %}
    {{ parent() }}

    {# Note that this global JavaScript variable must be set before you include the CKEditor 4 code. #}
    {# vendor/friendsofsymfony/ckeditor-bundle/src/Resources/views/Form/ckeditor_widget.html.twig #}
    <script type="text/javascript">
        var CKEDITOR_BASEPATH = "{{ ckeditor_base_path('bundles/fosckeditor/') }}";
    </script>
{% endblock %}

i "think" that it may be related to the fact that the CKEditor assets are not native but installed afterwards?

Temporary solution

The only working solution I found was to install the CKEditor in another folder and reference to the assets as they were static files:

composer.json:

"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd",
        "importmap:install": "symfony-cmd",
        "ckeditor:install %PUBLIC_DIR%/bundles/fosckeditor --tag=4.22.1": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts"
    ],
    "post-update-cmd": [
        "@auto-scripts"
    ]
},

Steps to reproduce

Install Sonata Formatter in an empty Symfony 6.4 project with AssetMapper (--webapp).

Expected results

The files are resolved correctly such as:

<script src="[/assets/bundles/fosckeditor/ckeditor-8d02c84b1cd4dfccf55df7b786b7e6dc.js](https://localhost:8000/assets/bundles/fosckeditor/ckeditor-8d02c84b1cd4dfccf55df7b786b7e6dc.js)"></script>

Actual results

The files are not found and give a 404 Not Found.

Thanks.

lukepass avatar Mar 08 '24 13:03 lukepass