Rectangle 27 0

The _sonata_admin (route) attribute is used by SonataAdminBundle to get the required admin instance ($this->admin) and be able to configure/process your requests:

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('approve_order', $this->getRouterIdParameter().'/approve');
}

You need to add also the _sonata_admin code to generate the right request to approveOrderAction():

{{ render(controller('QiBssFrontendBundle:PmodOrderCRUD:approveOrder', { 'id': object.id, '_sonata_admin': '...' })) }}

Let's make a simple example:

You have an Order entity and its admin class: OrderAdmin into PurchaseBundle, so this is the Sonata's service definition for OrderAdmin class (Yaml):

services:
    purchase_bundle.admin.order_admin:
        class: PurchaseBundle\Admin\OrderAdmin
        arguments:
            - ~
            - PurchaseBundle\Entity\Order
            - ~
        tags:
            - { name: 'sonata.admin', manager_type: orm }

Now, based on your own approveOrderAction(), you can render this action in the follows way:

{{ render(controller('PurchaseBundle:OrderAdmin:approveOrder', { 'id': object.id, '_sonata_admin': 'purchase_bundle.admin.order_admin' })) }}
'purchase_bundle.admin.order_admin'

symfony - Adding a custom form inside the show template of a Sonata Ad...

symfony sonata-admin symfony-sonata sonata
Rectangle 27 0

Based on the answer of @parissss although it was wrong, here is a working solution. The Sonata input field will show all the roles that are under the given role if you save one role.

On the other hand, in the database will be stored only the most important role. But that makes absolute sense in the Sf way.

protected function configureFormFields(FormMapper $formMapper) {
// ..
$container = $this->getConfigurationPool()->getContainer();
$roles = $container->getParameter('security.role_hierarchy.roles');

$rolesChoices = self::flattenRoles($roles);

$formMapper
    //...
    ->add('roles', 'choice', array(
           'choices'  => $rolesChoices,
           'multiple' => true
        )
    );

and in another method...

/**
 * Turns the role's array keys into string <ROLES_NAME> keys.
 * @todo Move to convenience or make it recursive ? ;-)
 */
protected static function flattenRoles($rolesHierarchy) 
{
    $flatRoles = array();
    foreach($rolesHierarchy as $roles) {

        if(empty($roles)) {
            continue;
        }

        foreach($roles as $role) {
            if(!isset($flatRoles[$role])) {
                $flatRoles[$role] = $role;
            }
        }
    }

    return $flatRoles;
}

See it in action :

symfony2 - set role for users in edit form of sonata admin - Stack Ove...

symfony2 symfony-2.1 fosuserbundle sonata-admin user-roles
Rectangle 27 0

Check your twig file to see which block is still being used. The sonata blocks section only has sonata.admin.block.admin_list which should be loaded with the defaults.

You can get error feedback by enabling filters: [sorry this is my first resonse. How do you get the formatting to work?] sonata_block: default_contexts: [cms] exception: default: filter: debug_only renderer: throw filters: debug_only: sonata.block.exception.filter.debug_only ignore_block_exception: sonata.block.exception.filter.ignore_block_exception keep_all: sonata.block.exception.filter.keep_all keep_none: sonata.block.exception.filter.keep_none renderers: inline: sonata.block.exception.renderer.inline inline_debug: sonata.block.exception.renderer.inline_debug throw: sonata.block.exception.renderer.throw

Then add this line directly under the admin block: blocks: sonata.admin.block.admin_list: contexts: [admin] exception: { filter: keep_all, renderer: inline }

php - Symfony2 - Sonata adminbundle menu won't show - Stack Overflow

php symfony2 sonata-admin symfony-sonata
Rectangle 27 0

Show images in Sonata

1. When you delete a product object, all images related to that product be deleted.

You can easily manage this kind of deletion with the orphanRemoval="true"

<?php
class Product{
    [...]
    /**
     * @ORM\OneToMany(targetEntity="Event", mappedBy="day", orphanRemoval="true", cascade={"all"})
     */
    private $images;
    [...]
}

in yml, the configuration looks like:

oneToMany:
  images:
    targetEntity: Image
    orphanRemoval: true
    mappedBy: product

2. When you are in show product page or add new product page the picture of all of images display on page.

You have to use a sonata_type_collection in your configureFormFields or your configureShowFields method of your ProductAdmin class:

<?php
class ProductAdmin{
    [...]
    protected function configureFormFields(FormMapper $formMapper) {
        $formMapper
            ->with('tab_images')
                ->add('images', 'sonata_type_collection', array(
                    'required' => false,
                    'cascade_validation' => true,
                    'by_reference' => false,
                ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                ))
            ->end()
        ;
    }
    [...]
}

Then provide a ImageAdmin with all you need to upload files. You might have to change settings because i took it from a personal project and i don't know if it's totally adapted to your needs.

first configure your ImageAdmin as follow:

class ImageAdmin extends Admin
{
    [...]
    protected function configureShowFields(ShowMapper $showMapper) {
        $showMapper
            ->add('myImageAttr', 'image', array(
                'prefix' => '/',
            ))
        ;
    }
    [...]
    protected function configureListFields(ListMapper $listMapper) {
        $listMapper
            ->add('myImageAttr', 'image', array(
                'prefix' => '/',
                'width' => 100
            ))
        ;
    }
}

then create template for your list_image and you show_image types:

{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}
{% block field %}
    {% if value %}
        <img src="{% if field_description.options.prefix %}{{ field_description.options.prefix }}{% endif %}{{ value }}" />
    {% endif %}
{% endblock %}
{% extends admin.getTemplate('base_list_field') %}

{% block field%}
{% spaceless %}
    {% set width = field_description.options.width is defined ? field_description.options.width : 50 %}
    {% set height = field_description.options.height is defined ? field_description.options.height : 50 %}
    {% if value %}
        <img src="{% if field_description.options.prefix is defined %}{{ field_description.options.prefix }}{% endif %}{{ value }}" 
            style="max-width:{{ width }}px; max-height:{{ height }}px;" />
    {% else %}
        <div class="no-image" style="width:{{ width }}px; height:{{ height }}px;"></div>
    {% endif %}
{% endspaceless %}
{% endblock %}
sonata_doctrine_orm_admin:
  templates:
    types:
      show:
        image: YourBundle:CRUD:show_image.html.twig
      list:
        image: YourBundle:CRUD:list_image.html.twig

My second problem is that in product show page it shows me the name of image instead of its preview. Of course it's because when I override the __toString function in Image class I returned the title of image. I am trying to return an html like <img src='...'> for previewing but it does not work

Post the Twig code that isn't working. For your other one, just add orphanRemoval: true under the entity under the oneToMany section of your .yml

i updated the answer for the orm option in yml (same as @JasonRoman solution), and i'll update the answer for your second question

symfony2 - Managing one to many entities in sonata admin bundle - Stac...

symfony2 sonata-admin
Rectangle 27 0

To prevent sonata from showing 0000000037b8f75700000000ca0fce3d you need to add toString methode to your entities. For the problem of the dashboard links can you show me the dashboard configuration in your config.yml i think that you reffer to the same service in the configuration. Finaly if Sonata return an errors mesasge you have to check your logic , i mean that you have to check your Assert and check the object after the form submit event.

Here is the error image: s29.postimg.org/v50z3msqf/Sans_titre1.png I have add the function: public function __toString() { return (string) $this->nom; } in my questionnaire entity, it will work and erase the top messge, but the error at the bottom stay

ToString methode must be like this : public function __tostring() { return $this->nom; }

Sorry, I have edited my message jsut before you sent. I have added the config.yml editing my question a the top.

im talking about the dashboard configuration under sonata admin in config.yml. Try to add those lines in your user admin protected $baseRouteName = 'sonata_admin_user'; protected $baseRoutePattern = 'admin/user';

I don't have another one in the app/config/config.yml file. I have tried to change these variables, but it doesn't have any effect. my path is: localhost/questionnaire/web/app_dev.php/admin/your_name/create The variables was: protected $baseRouteName= 'your_name'; protected $baseRoutePattern= 'your_name';

php - sonata create entity admin dashboard - Stack Overflow

php symfony2 sonata-admin
Rectangle 27 0

i found the solution for this i need just to define the groups on the config.yml Like this

dashboard:
    groups:
        Content:
            label: Content
            items:
                - sonata.admin.pages
                - sonata.admin.menus
            roles: [ ROLE_ADMIN_CONTENT, ROLE_SUPER_ADMIN ]

symfony2 - show/hide some menus depend on the Sonata admin ROLE - Stac...

symfony2 fosuserbundle sonata-admin symfony-2.3 symfony-sonata
Rectangle 27 0

Found it, just add the correct action to the configureSideMenu function

protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
    if (!$childAdmin && !in_array($action, array('edit', 'show'))) {
        return;
    }
    $admin = $this->isChild() ? $this->getParent() : $this;
    $id = $admin->getRequest()->get('id');

    $menu->addChild(
       'view',
        array('uri' => $admin->generateUrl('show', array('id' => $id)))
    );

    $menu->addChild(
        'replies',
        array('uri' => $admin->generateUrl('sonata.admin.module.application.replies.list', array('id' => $id)))
    );
}

symfony2 - Show sidebar in show view - Sonata admin bundle - Stack Ove...

symfony2 sonata-admin
Rectangle 27 0

Since you have a property in your parent entity which refers to your child entity you can call child fields in your configureShowFields methods to show fields from your child entity

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->with('Child Fields', array('collapsed' => true))
        ->add('child.propertyName',null,array('label'=>'Label'))
        ->add('child.anotherPropertyName',null,array('label'=>'Label'))
        ... ;
}

OMG that was so easy... thanks!

php - How to embed child entities in sonata admin show views? - Stack ...

php symfony2 doctrine sonata-admin
Rectangle 27 0

In base_edit.html.twig from Sonata Bundle I have if statement, which check for me, that user has appropriate role to show concrete fields in form.

It's "hard coding", but I didn't invent anything else.

symfony2 - Extend SonataProject? Form fields or different forms for cu...

symfony2