stijnhau / news-bundle
News and announcements for Symfony apps: posts with optional translations, a publish/expiry window, admin-only writing (via stijnhau/moderation-bundle) and optional ready-made pages.
Requires
- php: >=8.4
- doctrine/doctrine-bundle: ^3.4
- doctrine/orm: ^3.8
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- stijnhau/moderation-bundle: ^0.1.1
- symfony/clock: ^8.1
- symfony/config: ^8.1
- symfony/dependency-injection: ^8.1
- symfony/framework-bundle: ^8.1
- symfony/html-sanitizer: ^8.1
- symfony/http-kernel: ^8.1
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^13.4
- symfony/browser-kit: ^8.1
- symfony/css-selector: ^8.1
- symfony/event-dispatcher: ^8.1
- symfony/form: ^8.1
- symfony/security-bundle: ^8.1
- symfony/translation: ^8.1
- symfony/twig-bundle: ^8.1
- symfony/yaml: ^8.1
Suggests
- symfony/form: For the ready-made news and news admin pages (stijnhau_news.ui)
- symfony/twig-bundle: For the ready-made news and news admin pages (stijnhau_news.ui)
Provides
None
Conflicts
None
Replaces
None
README
News and announcements for Symfony apps, shared by CityWars, Crime-Life and
F1Manager. Posts can have translations and a publish/expiry window, so one
post works as a news item or as a time-boxed banner. Only admins may write.
It builds on stijnhau/moderation-bundle:
authors are its users, isAdmin() decides who may write, and every change
goes in its audit log.
| Piece | What it does |
|---|---|
Model\AbstractNewsPost |
Doctrine mapped superclass: title, body, translations (locale => title/body), author, published at (future = scheduled), expires at, created and updated times. |
Service\NewsAdmin |
publish(), update(), delete(): admins only, with every rule; bodies sanitized (body_format: html) or kept as plain text. |
Query\NewsReader |
Live posts newest first (paged, or all of them for a banner), all posts for the admin, findLive(). |
| Twig UI (optional) | /news and an admin (list, write, edit, delete) with ui.enabled: true. |
Install
composer require stijnhau/news-bundle
Register and configure stijnhau/moderation-bundle first (its user_class is
the news authors' class), then:
#[ORM\Entity] class NewsPost extends AbstractNewsPost {}
stijnhau_news: post_class: App\Entity\NewsPost per_page: 10 body_format: html # html (sanitized) or text body_max_length: 20000 sanitizer: ~ # an HtmlSanitizerInterface service id locales: [fr, nl] # translations a post may have audit_log: true # the moderation bundle's audit log actions: { published: news.published, updated: news.updated, deleted: news.deleted } ui: enabled: false admin_role: ROLE_ADMIN
Then run bin/console make:migration.
With the UI enabled, import the routes:
stijnhau_news: resource: '@StijnhauNewsBundle/config/routes.php' prefix: /news trailing_slash_on_root: false stijnhau_news_admin: resource: '@StijnhauNewsBundle/config/routes_admin.php' prefix: /admin/news trailing_slash_on_root: false
To use your own layout, override templates/bundles/StijnhauNewsBundle/news/layout.html.twig
(keep the news_title and news blocks) and admin/layout.html.twig (keep
news_title and news_admin). Body fields carry data-news-editor for a
rich-text editor.
Errors are NewsExceptions, with keys in the stijnhau_news domain (en/nl/fr included).
A NewsChangedEvent is dispatched for every change.
License
MIT, see LICENSE.