Overriding Registration Form (SonataUserBundle) since Symfony2 Update to 2.8 - symfony

After updating symfony2 with the dependencies to 2.8 I get the following error message when trying to override the sonata user bundle registration form:
The field type "Sonata\UserBundle\Form\Type\RegistrationFormType" is not registered with the service container.
If I switch back to Symfony 2.7 everything works again.
My services.yml:
sonata.user.registration.form.type:
class: My\Bundle\Form\Type\RegistrationFormType
arguments: [ "%fos_user.model.user.class%" , "#service_container"]
tags:
- { name: form.type, alias: sonata_user_registration }
In my controller the following line triggers the error:
$form = $this->container->get( 'sonata.user.registration.form' );
Unfortunately I couldn't find any resources on this subject (i.e. if there are any changes in overriding the registration form since the latest version)

Ok, this isn't a bug, but a new feature. You have to use the build() and boot() methods in your bundle to register your FormType via FormHelper::registerFormTypeMapping.

Related

Routing changes when upgrading easyadmin bundle symfony

I am in charge of upgrading easyadmin bundle on an app that was previously built using symfony v4.4.19. Initially we had: easycorp/easyadmin-bundle v2.3.12. Then, we decided to upgrade the easyadmin bundle to v3 because we faced some issues when enabling/disabling a boolean property from the list view.
When I was using the v2 :
php bin/console debug:router showed a route called easyadmin with a path /myworkshop/ .
I had no Dashboard controller nor NecklaceCrudController, I simply had a controlller called
AccessoriesController.php with several actions like deleteAction that is executed when the user
deletes an entity, editAction when the user edits an entity, SearchAction ...
In the deleteAction there is this line of code:
return $this->redirect($this->generateUrl('easyadmin', array('action' => 'list', 'entity'=> $this->entity['name'])));
so the url would become something like this
/myworkshop/?action=list&entity=necklace
To open the easy admin interface I have to click on a menu link whose link is :
->createItem('Visit my workshop', ['route' => 'easyadmin']);
When I open this interface /references, I get the list of the different entities in my app, if
I select one, I see the list view and I can edit one entity successfully but I cannot
enable/disable boolean properties from the list view as mentioned earlier.
We specified /myworkshop instead of /admin in app>config>routing.yml
# easy admin
easy_admin_bundle:
resource: "#myShop/Controller/AccessoriesController.php"
type: annotation
prefix: /myworkshop```
- A custom css was successfully employed in : app>config>config.yml
easy_admin:
design:
assets:
css:
- 'bundles/css/easyadmin.css
When I upgraded to v3:
php bin/console debug:router showed a route called myshop_admin_dashboard_index (which was
automatically generated) with a path /easyadmin.
Dashboard controller and NecklaceCrudController were created, The DashboardController only has configureCrud() and configureMenuItems() functions. The latter contains the links yield MenuItem::linkToCrud . Question 1 : In version 2 no menu links where created explicitely like here, so I was wondering how was the complete list of entities correctly showing up on my application interface?
In DashboardController there is no index () function nor a route nor a link just configureCrud() and configureMenuItems() functions.
I want to keep the AccessoriesController.php with his several actions but now, with the new route and path, it is completely being ignored. Question 2 : Is there something that I have to change in the generateUrl part? can someone give me an example of what this will become if I opt for adminUrlgenerator like I read in the documentation?
The routing.yml file remains the same however, my easyadmin interface appears only when visiting this link /easyadmin instead of /myworkshop . Question 3: I want to keep the /workshop url , what should I do in addition to keeping the routing.yml as it is now?
I wish we could change myshop_admin_dashboard_index to easyadmin and /easyadmin to my /myworkshop as it was in version 2, because there are many parts in AccessoriesController where I use $this->generateUrl('easyadmin',
Question 4: The css is no longer applicable any idea why? could be related to the AccessoriesController that is not currently being taken into account.
Well, you can add the index method to your dashboard controller with route annotation to change the route
class DashboardController extends AbstractDashboardController
{
/**
* #Route("/myworkshop", name="admin")
*/
public function index(): Response
{
return $this->render('dashboard/index.html.twig');
}
You can add any route to easyadmin menu like this
class DashboardController extends AbstractDashboardController
{
public function configureMenuItems(): iterable
{
yield MenuItem::linktoRoute('Some Route', 'fa fa-info', 'route_name_here');
#...
}
}
You can add any CSS/js file too
class DashboardController extends AbstractDashboardController
{
public function configureAssets(): Assets
{
return Assets::new()
->addCssFile('build/admin.css')
->addJsFile('build/admin.js')
;
}
}

Doctrine Entity listener in symfony

Iam a little confused while I try to set up a doctrine entity listener with injected parameters in symfony.
First there is this page where Entity Listeners are barely mentioned:
https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html
In Doctrine 2.4, a feature called Entity Listeners was introduced. It
is a lifecycle listener class used for an entity. You can read about
it in the Doctrine Documentation.
Then here is explained, that I only have to add a service with the right tags and that I don't have to register the listener in the entity when using doctrine 2.5 (which I do):
https://symfony.com/doc/master/bundles/DoctrineBundle/entity-listeners.html
If you use a version of doctrine/orm < 2.5 you have to register the
entity listener in your entity as well:
But the latter is not mentioned on the doctrine page:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#entity-listeners
However, following the doctrine page, I also added to my entity yaml:
AppBundle\Entity\File:
type: entity
entityListeners:
AppBundle\EventListener\FileListener:
postRemove: [postRemove]
And that is my Listener Class (still for testing):
class FileListener {
public function postRemove(File $file) {
dump($file); die();
}
}
That is what I set up in the service.yml:
file_listener:
class: AppBundle\EventListener\FileListener
arguments: ['%path_image%', '%path_thumb%', '%path_preview%']
tags:
- { name: doctrine.orm.entity_listener }
- { name: doctrine.orm.entity_listener, entity_manager: custom }
But when deleting a File Entity, the Listener Method is not called.

Symfony2 template configuration from routing.yml

When dealing with the Sylius e-commerce bundles I have found what seems to be a way of configurig the template for a route, that I didn't know:
I have tested in a fresh Symfony RC 2.2.0 with vendors installation.
This would be in the routing.yml
_welcome:
pattern: /
defaults:
_controller: AcmeDemoBundle:Welcome:index
_template: AcmeDemoBundle:Welcome:index # added by me
this generates an error:
FatalErrorException: Error: Call to a member function getTemplate() on
a non-object in
.... \vendor\sensio\framework-extra-bundle\Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener.php
line 62
now, in TemplateListener, what we have is:
if (!$configuration = $request->attributes->get('_template')) {
return;
}
if (!$configuration->getTemplate()) {
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$configuration->setTemplate($guesser->guessTemplateName($controller, $request, $configuration->getEngine()));
}
$configuration is a String, actually the template I put in routing.yml (AcmeDemoBundle:Welcome:index). Checked by adding a var_dump and also inspecting ParameterBag -> get method which is what $request->attributes is.
So. Why TemplateListener is expecting an object? What am I missing? Am I missconfiguring in routing.yml?
This parameter is not available in Symfony itself.
Feature is provided by SyliusResourceBundle and available only in Sylius controllers.
And apparently _template request attribute conflicts with SensioFrameworkExtraBundle, which uses same name to store object.
We have to move those parameters one config node deeper, to avoid such problems in future.
You can keep an eye on the https://github.com/Sylius/SyliusResourceBundle repository, fix should arrive today.

How to fix "This repository can be attached only to ORM sortable listener" error in Gedmo sortable?

When usieing StofDoctrineExtensions (which is a Symfony2 port of Gedmo Doctrine Extensions) Sortable behaviour I kept on getting this error:
This repository can be attached only to ORM sortable listener
Since I could not easily find the answer in official docs I'm leaving an answer here for future reference.
You need to enable any listeners you are using. In this case, Sortable.
stof_doctrine_extensions:
default_locale: en_US
orm:
default:
sortable: true
For Symfony 4, add this configuration in /config/packages/stof_doctrine_extensions.yaml. For older versions of Symfony, add it to config.yml.
In order to use Sortable behaviour you need to add an event listener to your bundle's boot method
<?php
namespace Acme\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeDemoBundle extends Bundle
{
public function boot()
{
// get the doctrine 2 entity manager
$em = $this->container->get('doctrine.orm.default_entity_manager');
// get the event manager
$evm = $em->getEventManager();
$evm->addEventSubscriber(new \Gedmo\Sortable\SortableListener);
}
}

Sonata Admin configuration with FosUser but *not* SonataUser

As per the title I'm using Sonata Admin and FosUser.
Sonata Admin is working fine for my normal entities; I can do the usual CRUD things.
I then followed this example in the docs for hooking into FosUser
My services.yml is thus:
sonata.admin.user:
class: Company\MyBundle\Admin\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Administration", label: "Users" }
arguments: [ null, Company\UserBundle\Entity\User, SonataAdminBundle:CRUD ]
calls:
- [ setUserManager, [ #fos_user.user_manager ] ]
... and I've created UserAdmin.php:
namespace Company\MyBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use FOS\UserBundle\Model\UserManagerInterface;
class UserAdmin extends Admin
{
I get a user link in the dashboard but when I follow create new or click to edit my fixtures user (BTW the rendered route is http://dev.url/app_dev.php/admin/company/user/user/create) I get a 500 server error saying "Class does not exist":
in /my/path/company/codebase/vendor/doctrine/common/lib/Doctrine/Common/Persistence/AbstractManagerRegistry.php at line 199
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
$proxyClass = new \ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$class = $proxyClass->getParentClass()->getName();
}
I've fiddled with some of the service settings, the only things I can see different to the example are:
I've changed the namespace to point to my admin class folder
%fos.user.admin.user.entity% wasn't configured anywhere so I've hardcoded it to be Company\UserBundle\Entity\User
I did start to look at adding the SonataUser bundle but as I'm still finding my feet with Symfony2 I figured I'd keep things simple for now and get an unserstanding of what's going wrong here. given the example doesn't mention it as a dependency I assume I can run without it?

Resources