sonata-project/news-bundle
SonataNewsBundle adds news/blog features to Symfony with Sonata integration. Includes admin management, posts, categories, comments, and RSS support. Note: the repository is abandoned and currently has no active maintenance or support.
Installation:
composer require sonata-project/news-bundle
Add to config/bundles.php:
return [
// ...
Sonata\NewsBundle\SonataNewsBundle::class => ['all' => true],
];
Database Migration: Run the SonataNewsBundle’s migrations (or manually create tables based on schema):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Create a news item via the admin interface (auto-generated by SonataAdminBundle):
php bin/console sonata:admin:generate
Access /admin/news to create/edit news entries.
config/packages/sonata_admin.yaml (or config/packages/sonata_news.yaml if using standalone).templates/SonataNewsBundle/ (e.g., default/index.html.twig).config/services.yaml (e.g., sonata.news.manager.news).NewsAdmin class to add custom fields:
// src/Admin/NewsAdmin.php
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\NewsBundle\Admin\NewsAdmin as BaseNewsAdmin;
class NewsAdmin extends BaseNewsAdmin {
protected function configureFormFields(FormMapper $formMapper) {
parent::configureFormFields($formMapper);
$formMapper->add('custom_field', TextType::class);
}
}
config/packages/sonata_admin.yaml:
sonata_admin:
options:
delete:
confirmation: true
list:
actions:
- { name: 'Reorder', label: 'sonata_news.admin.reorder', icon: '<i class="fa fa-sort"></i>' }
sonata_news_list Twig function in templates:
{{ sonata_news_list({
'where': {'o.enabled': 1},
'orderBy': {'o.createdAt': 'DESC'},
'limit': 5
}) }}
sonata_news_show with slug:
{{ path('sonata_news_show', {'slug': news.slug}) }}
{% paginate newsItems %}
{% for item in newsItems %}
{{ item.title }}
{% endfor %}
{% endpaginate %}
# config/packages/sonata_api.yaml
sonata_api:
resources:
- { resource: 'Sonata\NewsBundle\Entity\News', group_name: 'news', formats: [json] }
sonata.news.pre_persist or sonata.news.post_persist events:
// src/EventListener/NewsListener.php
use Sonata\NewsBundle\Event\NewsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NewsListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
NewsEvent::PRE_PERSIST => 'onPrePersist',
];
}
public function onPrePersist(NewsEvent $event) {
$news = $event->getSubject();
$news->setSeoTitle('Default SEO Title');
}
}
config/packages/sonata_news.yaml for bundle configuration (e.g., enable/disable features):
sonata_news:
class:
news: App\Entity\News
media: Sonata\MediaBundle\Entity\Media
db_driver: doctrine_orm # or doctrine_mongodb
persist_media: true
sonata_media:
db_driver: doctrine_orm
context:
default:
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
translations/messages.en.yaml:
sonata_news:
admin:
label_news: "Blog Post"
label_news_plural: "Blog Posts"
Deprecated Bundle:
composer.json:
"sonata-project/news-bundle": "3.11"
Doctrine Migrations:
News entity. Always back up your database before migrations.Admin Route Conflicts:
_sonata_admin prefix or override routing:
# config/routes.yaml
sonata_news:
resource: '@SonataNewsBundle/Resources/config/routing/admin.xml'
prefix: /admin/news
Media Handling:
persist_media: false, media files won’t be saved to the database. Ensure proper file storage (e.g., AWS S3) is configured in sonata_media.Caching Issues:
php bin/console cache:clear
Admin Panel Not Showing:
SonataAdminBundle is installed and configured:
composer require sonata-project/admin-bundle
sonata_admin configuration in config/packages/sonata_admin.yaml:
sonata_admin:
security:
handler: sonata.admin.security.handler.acl
templates:
layout: 'SonataAdminBundle::standard_layout.html.twig'
News Not Displaying:
News entity is properly mapped and the enabled field is set to true:
// src/Entity/News.php
/** @ORM\Column(type="boolean") */
private $enabled = true;
newsItems not passed to the template).Database Errors:
php bin/console doctrine:schema:validate to check schema compatibility.doctrine_mongodb_odm is installed and configured.Custom News Entity:
News entity to add fields:
// src/Entity/News.php
use Sonata\NewsBundle\Entity\BaseNews;
class News extends BaseNews {
/** @ORM\Column(type="string", length=255) */
private $customField;
}
sonata_news.class.news in config to point to your entity.Custom Repository:
// src/Repository/NewsRepository.php
use Sonata\NewsBundle\Repository\NewsRepository as BaseNewsRepository;
class NewsRepository extends BaseNewsRepository {
public function findPublished() {
return $this->createQueryBuilder('o')
->where('o.enabled = :enabled')
->setParameter('enabled', true)
->orderBy('o.createdAt', 'DESC')
->getQuery()
->getResult();
}
}
services.yaml:
services:
App\Repository\NewsRepository:
parent: sonata.news.repository.news
tags: ['doctrine.repository_service']
Custom Templates:
templates/SonataNewsBundle/:
templates
How can I help you explore Laravel packages today?