Skip to content

Commit

Permalink
update documentation for symfony4
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored and jordisala1991 committed Mar 10, 2018
1 parent ecae9ab commit 388af4c
Show file tree
Hide file tree
Showing 40 changed files with 663 additions and 574 deletions.
41 changes: 23 additions & 18 deletions docs/cookbook/recipe_custom_action.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ Creating a Custom Admin Action
==============================

This is a full working example of creating a custom list action for SonataAdmin.
The example is based on an existing ``CarAdmin`` class in an ``AppBundle``.
The example is based on an existing ``CarAdmin`` class in a ``App`` namespace.
It is assumed you already have an admin service up and running.

.. note::
This article assumes you are using Symfony 4. Using Symfony 2.8 or 3
will require to slightly modify some namespaces and paths when creating
entities and admins.

The recipe
----------

Expand All @@ -25,9 +30,9 @@ First you need to create your own Controller extending the one from SonataAdmin
.. code-block:: php
<?php
// src/AppBundle/Controller/CRUDController.php
// src/Controller/CRUDController.php
namespace AppBundle\Controller;
namespace App\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
Expand All @@ -46,30 +51,30 @@ Either by using XML:

.. code-block:: xml
<!-- src/AppBundle/Resources/config/admin.xml -->
<!-- src/Resources/config/admin.xml -->
<service id="app.admin.car" class="AppBundle\Admin\CarAdmin">
<service id="app.admin.car" class="App\Admin\CarAdmin">
<tag name="sonata.admin" manager_type="orm" group="Demo" label="Car" />
<argument />
<argument>AppBundle\Entity\Car</argument>
<argument>AppBundle:CRUD</argument>
<argument>App\Entity\Car</argument>
<argument>App\Controller\CRUDController</argument>
</service>
or by adding it to your ``admin.yml``:

.. code-block:: yaml
# src/AppBundle/Resources/config/admin.yml
# src/Resources/config/admin.yml
services:
app.admin.car:
class: AppBundle\Admin\CarAdmin
class: App\Admin\CarAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: Demo, label: Car }
arguments:
- null
- AppBundle\Entity\Car
- AppBundle:CRUD
- App\Entity\Car
- App\Controller\CRUDController
public: true
For more information about service configuration please refer to Step 3 of :doc:`../getting_started/creating_an_admin`
Expand All @@ -83,9 +88,9 @@ to implement a ``clone`` action.
.. code-block:: php
<?php
// src/AppBundle/Controller/CRUDController.php
// src/Controller/CRUDController.php
namespace AppBundle\Controller;
namespace App\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
Expand Down Expand Up @@ -151,7 +156,7 @@ Admin Controller.

.. code-block:: html+jinja

{# src/AppBundle/Resources/views/CRUD/list__action_clone.html.twig #}
{# templates/CRUD/list__action_clone.html.twig #}

<a class="btn btn-sm" href="{{ admin.generateObjectUrl('clone', object) }}">clone</a>

Expand Down Expand Up @@ -207,9 +212,9 @@ The full ``CarAdmin.php`` example looks like this:
.. code-block:: php
<?php
// src/AppBundle/Admin/CarAdmin.php
// src/Admin/CarAdmin.php
namespace AppBundle\Admin;
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
Expand Down Expand Up @@ -264,12 +269,12 @@ The full ``CarAdmin.php`` example looks like this:

If you want to render a custom controller action in a template by using the
render function in twig you need to add ``_sonata_admin`` as an attribute. For
example; ``{{ render(controller('AppBundle:XxxxCRUD:comment', {'_sonata_admin':
example; ``{{ render(controller('App:XxxxCRUD:comment', {'_sonata_admin':
'sonata.admin.xxxx' })) }}``. This has to be done because the moment the
rendering should happen the routing, which usually sets the value of this
parameter, is not involved at all, and then you will get an error "There is no
_sonata_admin defined for the controller
AppBundle\Controller\XxxxCRUDController and the current route ' '."
App\Controller\XxxxCRUDController and the current route ' '."

Custom Action without Entity
----------------------------
Expand Down
5 changes: 2 additions & 3 deletions docs/cookbook/recipe_customizing_a_mosaic_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ Block types:
- ``sonata_mosaic_description``: this block will be always on screen and should represent the entity's name.


The ``ObjectMetadata`` object is returned by the related admin class, for instance the MediaBundle defines the method as:

.. code-block:: jinja
The ``ObjectMetadata`` object is returned by the related admin class,
for instance the MediaBundle defines the method as::

<?php

Expand Down
21 changes: 13 additions & 8 deletions docs/cookbook/recipe_data_mapper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Using DataMapper to work with domain entities without per field setters

This is an example of using DataMapper with entities that avoid setters/getters for each field.

.. note::
This article assumes you are using Symfony 4. Using Symfony 2.8 or 3
will require to slightly modify some namespaces and paths when creating
entities and admins.

Pre-requisites
--------------

Expand All @@ -25,9 +30,9 @@ Example Entity
<?php
// src/AppBundle/Entity/Example.php
// src/Entity/Example.php
namespace AppBundle\Entity;
namespace App\Entity;
class Example
{
Expand Down Expand Up @@ -58,12 +63,12 @@ To be able to set entity data without the possibility to use setters a ``DataMap
<?php
// src/AppBundle/Form/DataMapper/ExampleDataMapper.php
// src/Form/DataMapper/ExampleDataMapper.php
namespace AppBundle\Form\DataMapper;
namespace App\Form\DataMapper;
use Symfony\Component\Form\DataMapperInterface;
use AppBundle\Entity\Example;
use App\Entity\Example;
class ExampleDataMapper implements DataMapperInterface
{
Expand Down Expand Up @@ -114,13 +119,13 @@ Now we need to configure the form to use our ``ExampleDataMapper``.
<?php
// src/AppBundle/Admin/ExampleAdmin.php
// src/Admin/ExampleAdmin.php
namespace AppBundle\Admin;
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use AppBundle\Form\DataMapper\ExampleDataMapper;
use App\Form\DataMapper\ExampleDataMapper;
class ExampleAdmin extends AbstractAdmin
{
Expand Down
38 changes: 26 additions & 12 deletions docs/cookbook/recipe_delete_field_group.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Deleting a Group of Fields from an Admin
========================================

In some cases, when you extend existing Admins, you might want to delete fields from the admin, or make them not show.
You could delete every field by hand, using the ``FormMapper``s ``remove`` method.
In some cases, when you extend existing Admins, you might want to delete
fields from the admin, or make them not show. You could delete every
field by hand, using the ``FormMapper``s ``remove`` method.

.. code-block:: php
class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin {
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
class UserAdmin extends SonataUserAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
parent::configureFormFields($formMapper);
Expand All @@ -22,24 +25,35 @@ You could delete every field by hand, using the ``FormMapper``s ``remove`` metho
}
This works, as long as the extended Admin does not use Groups to organize it's field. In the above example, we try to remove all fields from the User Admin, that comes with the SonataUserBundle.
However, since the fields we deleted, are all part of the 'Social' Group of the form, the fields will be deleted and the empty group will stay.
For this case, the FormMapper comes with a method, which allows you to get rid of a whole form group: ``removeGroup``
This works, as long as the extended Admin does not use Groups to organize its field.
In the above example, we try to remove all fields from the User Admin, that comes
with the SonataUserBundle. However, since the fields we deleted, are all part
of the 'Social' Group of the form, the fields will be deleted and the empty group will stay.
For this case, the FormMapper comes with a method, which allows you to get rid
of a whole form group: ``removeGroup``.

.. code-block:: php
class UserAdmin extends \Sonata\UserBundle\Admin\Model\UserAdmin {
use Sonata\UserBundle\Admin\Model\UserAdmin as SonataUserAdmin;
class UserAdmin extends SonataUserAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
parent::configureFormFields($formMapper);
$formMapper->removeGroup('Social', 'User');
}
}
This will remove the whole 'Social' group from the form, which happens to contain all the fields, we deleted manually in the first example. The second argument is the name of the tab, the group belongs to.
This is optional. However, when not provided, it will be assumed that you mean the 'default' tab. If the group is on another tab, it won't be removed, when this is not provided.
There is a third optional argument for the method, which let's you choose, whether or not, tabs are also removed, if you happen to remove all groups of a tab. This behaviour is disabled by default, but
can be enabled, by setting the third argument of ``removeGroup`` to ``true``
This will remove the whole 'Social' group from the form, which happens
to contain all the fields, we deleted manually in the first example.
The second argument is the name of the tab, the group belongs to.
This is optional. However, when not provided, it will be assumed that
you mean the 'default' tab. If the group is on another tab, it won't be
removed, when this is not provided. There is a third optional argument
for the method, which let's you choose, whether or not, tabs are also
removed, if you happen to remove all groups of a tab. This behaviour
is disabled by default, but can be enabled, by setting the third
argument of ``removeGroup`` to ``true``.
22 changes: 14 additions & 8 deletions docs/cookbook/recipe_dynamic_form_modification.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Modifying form fields dynamically depending on edited object
============================================================

.. note::
This article assumes you are using Symfony 4. Using Symfony 2.8 or 3
will require to slightly modify some namespaces and paths when creating
entities and admins.

It is a quite common situation when you need to modify your form's fields because
of edited object's properties or structure. Let us assume you only want to display
an admin form field for new objects and you do not want it to be shown for those
Expand All @@ -11,17 +16,18 @@ This is a way for you to accomplish this.
In your ``Admin`` class' ``configureFormFields`` method you are able to get the
current object by calling ``$this->getSubject()``. The value returned will be your
linked model. And another method ``isCurrentRoute`` for check the current request's route.
Then, you should be able to dynamically add needed fields to the form:

.. code-block:: php
Then, you should be able to dynamically add needed fields to the form::

<?php
// src/AppBundle/Admin/PostAdmin
// src/Admin/PostAdmin

namespace AppBundle\Admin;
namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class PostAdmin extends AbstractAdmin
{
Expand All @@ -31,19 +37,19 @@ Then, you should be able to dynamically add needed fields to the form:
{
// Description field will always be added to the form:
$formMapper
->add('description', 'textarea')
->add('description', TextareaType::class)
;

$subject = $this->getSubject();

if ($subject->isNew()) {
// The thumbnail field will only be added when the edited item is created
$formMapper->add('thumbnail', 'file');
$formMapper->add('thumbnail', FileType::class);
}

// Name field will be added only when create an item
if ($this->isCurrentRoute('create')) {
$formMapper->add('name', 'text');
$formMapper->add('name', TextType::class);
}
}
}

0 comments on commit 388af4c

Please sign in to comment.