SonataSeoBundle: Can't set Title at runtime
Environment
Sonata packages
$ composer show sonata-project/*
sonata-project/admin-bundle 3.4.0 The missing Symfony Admin Generator
sonata-project/block-bundle 3.0.1 Symfony SonataBlockBundle
sonata-project/cache 1.0.7 Cache library
sonata-project/cache-bundle 2.2.5 This bundle provides caching services
sonata-project/core-bundle 3.0.3 Symfony SonataCoreBundle
sonata-project/datagrid-bundle 2.2 Symfony SonataDatagridBundle
sonata-project/doctrine-extensions 1.0.2 Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.0.5 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle 2.1.10 Symfony SonataEasyExtendsBundle
sonata-project/exporter 1.5.0 Lightweight Exporter library
sonata-project/google-authenticator 1.0.2 Library to integrate Google Authenticator into a PHP project
sonata-project/intl-bundle 2.2.4 Symfony SonataIntlBundle
sonata-project/notification-bundle 3.0.0 Symfony SonataNotificationBundle
sonata-project/page-bundle 3.0.2 This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle 2.0.2 Symfony SonataSeoBundle
sonata-project/user-bundle 3.0.1 Symfony SonataUserBundle
Symfony packages
$ composer show symfony/*
symfony/assetic-bundle v2.8.0 Integrates Assetic into Symfony2
symfony/monolog-bundle 2.11.1 Symfony MonologBundle
symfony/phpunit-bridge v2.8.8 Symfony PHPUnit Bridge
symfony/polyfill-apcu v1.2.0 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-intl-icu v1.2.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-mbstring v1.2.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php54 v1.2.0 Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions
symfony/polyfill-php55 v1.2.0 Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions
symfony/polyfill-php56 v1.2.0 Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions
symfony/polyfill-php70 v1.2.0 Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-util v1.2.0 Symfony utilities for portability of PHP codes
symfony/security-acl v2.8.0 Symfony Security Component - ACL (Access Control List)
symfony/swiftmailer-bundle v2.3.11 Symfony SwiftmailerBundle
symfony/symfony v2.8.8 The Symfony PHP framework
PHP version
$ php -v
PHP 7.0.4-7ubuntu2.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Subject
I have a hybrid page, created and configured using SonataAdminBundle+SonataPageBundle. Suppose it’s has “old title” as title. And I want to change title at runtime (which will be later rendered by sonata_seo_title() Twig helper). Using SeoPage->setTitle() doesn’t works for me.
Steps to reproduce
Set title “old title” to some hybrid page through admin interface. In controller action for this page, try to change title:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MyController extends Controller
{
public function someAction(Request $request)
{
// ...
$seoPage = $this->get('sonata.seo.page');
$seoPage->setTitle('new title');
// ...
return $this->render('template.html.twig');
}
}
<!-- template.html.twig -->
<!DOCTYPE html>
<html>
<head>
{{ sonata_seo_title() }}
</head>
</html>
Expected results
sonata_seo_title() output:
<title>new title</title>
Actual results
<title>old title</title>
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 15 (7 by maintainers)
Thanks so much @Neodork! It worked perfectly and it’s really good to know, might be useful again in the future!
Hello @Aylarius
As mentioned by Rande, you are supposed to override the PageService. I can imagine that overriding it can be hard for new Sonata users. So I’ll go over the basics a bit to help you out.
In Sonata most important services and classes have been set as “Parameter”. This way you can override these services without accessing or modifying the vendor bundle. In our case we’re looking to override
sonata.page.service.default.class. Look at the example below.Note: It’s also possible to edit parameters programmatically.
Now that we set the new service make sure to create it. The simplest way to do this is copying
DefaultPageServicefrom the vendor bundle. And pasting it at the location specified in the parameters.yml file. In this case that would beNeodork\AppBundle\Page\Service\DefaultPageService.Now clear your cache and the new service should be in use. Modify it to suit your needs and you’re done!
@greg0ire the code is part of the PageBundle