sulu / SuluArticleBundle

Bundle for managing localized content-rich entities like blog-posts in the Sulu content management system
MIT License
52 stars 77 forks source link

How to extend the ArticleController #627

Open thenetexperts opened 1 year ago

thenetexperts commented 1 year ago
Q A
Bug? no
New Feature? yes
SuluArticleBundle Version latest
Sulu Version latest

I was looking for a way to add custom columns to different article types (news, reports, agencies ect), and found out I have to extend from the bundle's ArticleController.

The simple extension off of the class did not work, and I talked to @luca-rath on slack about it. As usual Luca had the solution ready.

During my research before asking on slack, I found many people asking for this as they failed doing so. My proposal is to extend the documentation to take note of this.

The working approach to make it work:

Add this section to /config/routes/sulu_article_admin.yaml

sulu_article.article:
    type: rest
    resource: App\Controller\Admin\ArticleController
    name_prefix: sulu_article.
    prefix: /admin/api

Add the extending class service definition explicitly to services.yaml

    App\Controller\Admin\ArticleController:
        public: true
        arguments:
            - '@fos_rest.view_handler'
            - '@sulu_document_manager.document_manager'
            - '@sulu.content.mapper'
            - '@sulu_document_manager.metadata_factory.base'
            - '@sulu_core.list_rest_helper'
            - '@es.manager.default'
            - '@sulu_article.view_document.factory'
            - '@form.factory'
            - '@sulu_hash.request_hash_checker'
            - '@sulu_security.security_checker'
            - '%sulu_article.display_tab_all%'
            - '@security.token_storage'
        tags: [{name: 'sulu.context', context: 'admin'}]

After this, clear cache and the new overriding class will be used for the articles section in the Admin.

(The approach is in line with the symfony documentation though for some reason the tags need to be added explicitly even if the src/Controller/Admin resource is assigned the tags already somewhere in the services.yaml).