I am having a weird situation which has halted my progress for the second day now and I am almost going bald from pulling my hair on this. I have a custom block on the sonata admin dashboard which is not being found when I try to load the page.
I have gone over the configuration a couple of times and maybe I am missing something which an extra pair of eyes may be able to spot which is why I am posting this question here.
I have built my block as below and saved it under src\AppBundle\Block\NumbersBlockService.php
namespace AppBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use DoctrineExtensions\Query\Mysql;
class NumbersBlockService extends BaseBlockService
{
......
}
Then defined my service in the service.yml file as below:
sonata.block.service.topnumbers:
class: AppBundle\Block\NumbersBlockService
arguments:
- sonata.block.service.topnumbers
- "#templating"
- "#doctrine.orm.entity_manager"
- "#security.token_storage"
tags:
- { name: sonata.block }
My config.yml file includes the block like this
sonata_block:
default_contexts: [cms]
blocks:
sonata.user.block.menu: # used to display the menu in profile pages
sonata.user.block.account: # used to display menu option (login option)
sonata.block.service.text: # used to if you plan to use Sonata user routes
sonata.block.service.topnumbers:
and finally, I position the block on top with the line below
sonata_admin:
dashboard:
blocks:
- { position: top, type: sonata.block.service.topnumbers, class: col-md-12}
I have checked the tutorial on creating a custom block here https://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html and everything seems to check out but I still get the following error below:
An exception has been thrown during the rendering of a template ("The block type "sonata.block.service.topnumbers" does not exist") in SonataAdminBundle:Core:dashboard.html.twig at line 60.
Someone please help put me out of my misery. Thanks in advance
Related
I am trying to override "new" template for EasyAdmin but system ignores template code.
Here is the relevant part of easy_admin.yaml
entities:
# List the entity class name you want to manage
Places:
class: App\Entity\Places
templates:
list: 'asdfasdf'
As you see, value of list attribute is invalid but system ignores it and works without error and i can not override the template.
Do you have a suggestion?
So, as I mentioned in the comment - for some reason easyadmin doesn't give any errors if specified template doesn't exists. So, you just need to place your new template in templates folder, in example, templates/admin/listPlaces.html.twig and then specify correct path in easyadmin's config file, in example:
entities:
Places:
class: App\Entity\Places
templates:
list: 'admin/listPlaces.html.twig'
If you use EasyAdmin 3.x, for that you can overwrite a specific template specifying it in your entity's Controller or you can create your own folder structures like symfony does.
In this example I am overwriting only the edit template for my 'studient' entity, if you want to change all the edits of your project you must do so by creating the folder structure as symfony does.
public function configureCrud(): Crud
{
return Crud::new()
->overrideTemplate('crud/edit', 'studient/edit.html.twig')
;
}
You can even combine both methods. Suppose that in addition to modifying only the template 'edit' of studient you want to modify the way in which easyadmin displays the flash messages for that you only have to create this structure in your templates folder: "templates\bundles\EasyAdminBundles\flash_messages.html.twig"
I leave the link with the documentation for EasyAdmin 3.x
overriding-templates
I try to override layout template in Sonata Admin. I did all steps founded in official documentation, but my changes don`t work.
I did:
Copied from vendor appropriate template (standard_layout.html.twig) to app/Resources/SonataAdminBundle/views/. I will check if I override here template my changes applied to all of Admins in project (I want have this change only in one Admin)
In next step I created new file in my Bundle (Name/InfoBundle/Resources/views/JobOffer) and add there my custom template: findCandidate.html.twig. Below is content of this file:
https://gist.github.com/anonymous/5f4780a1ae8d7329cd91
Added to bundle service:
name_info.admin.offers:
class: Name\InfoBundle\Admin\JobOfferAdmin
tags:
- {name: sonata.admin, manager_type: orm, group: Info, label: Job offers}
arguments: [~, Name\Info\Entity\JobOffer, NameInfoBundle:JobOffer]
calls:
- [ setTemplate, [findCandidate, NameInfoBundle:JobOffer:findCandidate.html.twig]]
After that my changes are not applied. So probably I made mistake in services or maybe I have to call this template also in controller? I am not sure where I make mistake. Could anyone help me?
Probably you mistyped a template placeholder in setTemplate function.
Try to set it like:
calls:
- [setTemplate, [layout, NameInfoBundle:JobOffer:findCandidate.html.twig]]
It will change a standard_layout only for the selected admin.
I'm trying to customize my dashboard of Sonata but although I can change the layout, I can't make the controller to be the one I want (and with that pass some other values). So sonata always loads the default on the CoreController and not the one that I want. Can you tell me what I have to do for changing the controller for the one I want?
This is a part of my config.yml:
sonata_admin:
title_logo: bundles/ebuigui/images/brand.png
templates:
dashboard: EBUIBackendBundle:CRUD:adminView.html.twig
list: EBUIBackendBundle:CRUD:list.html.twig
edit: EBUIBackendBundle:CRUD:create.html.twig
dashboard:
blocks:
# display a dashboard block
- { position: left, type: sonata.admin.block.admin_list }
sonata_block:
default_contexts: [cms]
blocks:
# Enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
it's better that you use eventListener to modif the logic of Sonata CoreController. If you want to override the core controller you can reffer to a question asked before
Anyone could tell me is it possible to create in Symfony2 next:
One main Bundle: functionality, layout, templates;
Many other Bundles: additional functionality, extending templates from main Bundle;
Tried to draw an example of idea https://www.dropbox.com/s/sv15wcxhdvamu9u/sym.png
More about idea: for example we have one layout.html.twig where we have 3 blocks. in each block we have some content from main Bundle and some special tag/mark/(controller call) - it will trigger all other bundles, and if any bundle has some content for that tag/mark/(controller call) it will return the result (e.g. block with buttons "toolbar", we have few buttons from main Bundle, and few buttons from other Bundles).
Is it possible?
How to delete unwanted fileds in the entity/model: facebook, twitter, bio, website from the Model/User.php ?
I try to ovveride the Model but it doesn't work.
I succed to override the entity, i added some new property, and it work, but want to delete unwanted stuff.
Thx, bye
You can create own user entity which will not extend SonataUserBundle User.php class.
If you used FOSUSerBundle:
class User extends FOS\UserBundle\Entity\User
or you can extend by default Symfony 2 Security User.php class.
Next you can configure SonataUserBundle to use your user entity:
sonata_user:
class:
user: MyBundle\Entity\User
Full configuration is here:
https://github.com/sonata-project/SonataUserBundle/blob/master/Resources/doc/reference/advanced_configuration.rst
If you used SonataUserBundle default controller to manage users you need to create own UserAdmin Class and configure sonata:
sonata_user:
admin: # Admin Classes
user:
class: MyBundle\Admin\UserAdmin
in fact, i got it.
I must to extend the model, not the entity, and it works !!!
I can remove unwanted stuff