diff --git a/docs/cookbook/recipe_custom_action.rst b/docs/cookbook/recipe_custom_action.rst index 1cfd1239a7..1b31c0cc0e 100644 --- a/docs/cookbook/recipe_custom_action.rst +++ b/docs/cookbook/recipe_custom_action.rst @@ -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 ---------- @@ -25,9 +30,9 @@ First you need to create your own Controller extending the one from SonataAdmin .. code-block:: php + - + - AppBundle\Entity\Car - AppBundle:CRUD + App\Entity\Car + App\Controller\CRUDController 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` @@ -83,9 +88,9 @@ to implement a ``clone`` action. .. code-block:: php clone @@ -207,9 +212,9 @@ The full ``CarAdmin.php`` example looks like this: .. code-block:: php 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``. diff --git a/docs/cookbook/recipe_dynamic_form_modification.rst b/docs/cookbook/recipe_dynamic_form_modification.rst index a4fa8b6a59..094a59acf0 100644 --- a/docs/cookbook/recipe_dynamic_form_modification.rst +++ b/docs/cookbook/recipe_dynamic_form_modification.rst @@ -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 @@ -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:: 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); } } } diff --git a/docs/cookbook/recipe_file_uploads.rst b/docs/cookbook/recipe_file_uploads.rst index 235158a698..d89325f851 100644 --- a/docs/cookbook/recipe_file_uploads.rst +++ b/docs/cookbook/recipe_file_uploads.rst @@ -4,6 +4,11 @@ Uploading and saving documents (including images) using DoctrineORM and SonataAd This is a full working example of a file upload management method using SonataAdmin with the DoctrineORM persistence layer. +.. 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 -------------- @@ -42,11 +47,11 @@ upload timestamp. .. code-block:: yaml - # src/AppBundle/Resources/config/Doctrine/Image.orm.yml + # src/Resources/config/Doctrine/Image.orm.yml AppBundleBundle\Entity\Image: type: entity - repositoryClass: AppBundle\Entity\Repositories\ImageRepository + repositoryClass: App\Entity\Repositories\ImageRepository table: images id: id: @@ -67,12 +72,10 @@ upload timestamp. prePersist: [ lifecycleFileUpload ] preUpdate: [ lifecycleFileUpload ] -We then have the following methods in our ``Image`` class to manage file uploads: - -.. code-block:: php +We then have the following methods in our ``Image`` class to manage file uploads:: add('file', 'file', [ + ->add('file', FileType::class, [ 'required' => false ]) @@ -225,12 +228,10 @@ Admin's lifecycle events to trigger the file management when needed. In this example we have a Page class which has three one-to-one Image relationships defined, linkedImage1 to linkedImage3. The PostAdmin class' form field configuration -looks like this: - -.. code-block:: php +looks like this:: getType() === 'sonata_type_admin' && ($associationMapping = $fieldDescription->getAssociationMapping()) && - $associationMapping['targetEntity'] === 'AppBundle\Entity\Image' + $associationMapping['targetEntity'] === 'App\Entity\Image' ) { $getter = 'get'.$fieldName; $setter = 'set'.$fieldName; diff --git a/docs/cookbook/recipe_improve_performance_large_datasets.rst b/docs/cookbook/recipe_improve_performance_large_datasets.rst index 0ce827b8b5..4b03a532f0 100644 --- a/docs/cookbook/recipe_improve_performance_large_datasets.rst +++ b/docs/cookbook/recipe_improve_performance_large_datasets.rst @@ -19,27 +19,27 @@ To use `SimplePager` in your admin just define ``pager_type`` inside the service .. code-block:: xml - + - + - AppBundle\Entity\Post + App\Entity\Post .. code-block:: yaml - # src/AppBundle/Resources/config/admin.yml + # config/services.yaml services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post", pager_type: "simple" } arguments: - ~ - - AppBundle\Entity\Post + - App\Entity\Post - ~ public: true diff --git a/docs/cookbook/recipe_knp_menu.rst b/docs/cookbook/recipe_knp_menu.rst index f6fc5abcb2..92ee255df1 100644 --- a/docs/cookbook/recipe_knp_menu.rst +++ b/docs/cookbook/recipe_knp_menu.rst @@ -2,16 +2,20 @@ KnpMenu ======= The admin comes with `KnpMenu`_ integration. -It integrates a menu with the KnpMenu library. This menu can be a SonataAdmin service, a menu created with a Knp menu provider or a route of a custom controller. +It integrates a menu with the KnpMenu library. This menu can be a SonataAdmin +service, a menu created with a Knp menu provider or a route of a custom controller. + +.. 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. Add a custom controller entry in the menu ----------------------------------------- To add a custom controller entry in the admin menu: -Create your controller: - -.. code-block:: php +Create your controller:: class BlogController { @@ -39,7 +43,7 @@ Add the controller route as an item of the menu: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -62,7 +66,7 @@ group roles will be checked. .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -80,13 +84,14 @@ group roles will be checked. label: Article roles: [ ROLE_ADMIN, ROLE_SONATA_ADMIN] -You can also override the template of knp_menu used by sonata. The default one is `@SonataAdmin/Menu/sonata_menu.html.twig`: +You can also override the template of knp_menu used by sonata. The default +one is `@SonataAdmin/Menu/sonata_menu.html.twig`: .. configuration-block:: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: templates: @@ -97,7 +102,11 @@ And voilà, now you have a menu group which contains a link to a sonata admin vi Using a menu provider --------------------- -As seen above, the main way to declare your menu is by declaring items in your sonata admin config file. In some case you may have to create a more complex menu depending on your business logic. This is possible by using a menu provider to populate a whole menu group. This is done with the ``provider`` config value. +As seen above, the main way to declare your menu is by declaring items +in your sonata admin config file. In some cases you may have to create a +more complex menu depending on your business logic. This is possible by +using a menu provider to populate a whole menu group. This is done with +the ``provider`` config value. The following configuration uses a menu provider to populate the menu group ``my_group``: @@ -105,7 +114,7 @@ The following configuration uses a menu provider to populate the menu group ``my .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -114,9 +123,12 @@ The following configuration uses a menu provider to populate the menu group ``my provider: 'MyBundle:MyMenuProvider:getMyMenu' icon: '' -With KnpMenuBundle you can create a custom menu by using a builder class or by declaring it as a service. Please see the `Knp documentation`_ for further information. +With KnpMenuBundle you can create a custom menu by using a builder class +or by declaring it as a service. Please see the `Knp documentation`_ for +further information. -In sonata, whatever the implementation you choose, you only have to provide the menu alias to the provider config key: +In sonata, whatever the implementation you choose, you only have to provide +the menu alias to the provider config key: * If you are using a builder class, your menu alias should be something like ``MyBundle:MyMenuProvider:getMyMenu``. * If you are using a service, your menu alias is the alias set in the ``knp_menu.menu`` tag. In the following example this is ``my_menu_alias``: @@ -129,19 +141,19 @@ In sonata, whatever the implementation you choose, you only have to provide the -Please note that when using the provider option, you can't set the menu label via the configuration. It is done in your custom menu. +Please note that when using the provider option, you can't set the menu +label via the configuration. It is done in your custom menu. Extending the menu ------------------ -You can modify the menu via events easily. You can register as many listeners as you want for the event with name ``sonata.admin.event.configure.menu.sidebar``: - -.. code-block:: php +You can modify the menu via events easily. You can register as many +listeners as you want for the event with name ``sonata.admin.event.configure.menu.sidebar``:: - + - + diff --git a/docs/cookbook/recipe_overwrite_admin_configuration.rst b/docs/cookbook/recipe_overwrite_admin_configuration.rst index 4ed8813046..5ffa1d7f70 100644 --- a/docs/cookbook/recipe_overwrite_admin_configuration.rst +++ b/docs/cookbook/recipe_overwrite_admin_configuration.rst @@ -1,9 +1,12 @@ Overwrite Admin Configuration ============================= -Sometime you might want to overwrite some Admin settings from vendors. This recipe will explain how to achieve this operation. However, keep in mind this operation is quite dangerous and might break code. +Sometimes you might want to overwrite some Admin settings from vendors. +This recipe will explain how to achieve this operation. However, keep +in mind this operation is quite dangerous and might break code. -From the configuration file, you can add a new section named ``admin_services`` with the following templates: +From the configuration file, you can add a new section named ``admin_services`` +with the following templates: .. code-block:: yaml diff --git a/docs/cookbook/recipe_select2.rst b/docs/cookbook/recipe_select2.rst index 27e465667d..47dda865a5 100644 --- a/docs/cookbook/recipe_select2.rst +++ b/docs/cookbook/recipe_select2.rst @@ -10,13 +10,13 @@ The select2 is enabled on all ``select`` form elements by default. Disable select2 --------------- -If you don't want to use select2 in your admin, you can disable it in ``config.yml``. +If you don't want to use select2 in your admin, you can disable it in configuration. .. configuration-block:: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: options: diff --git a/docs/cookbook/recipe_sortable_listing.rst b/docs/cookbook/recipe_sortable_listing.rst index dbc051b1eb..02d67743ca 100644 --- a/docs/cookbook/recipe_sortable_listing.rst +++ b/docs/cookbook/recipe_sortable_listing.rst @@ -3,6 +3,11 @@ Sortable behavior in admin listing This is a full working example of how to implement a sortable feature in your Sonata admin listing +.. 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. + Background ---------- @@ -84,14 +89,12 @@ and use the default twig template provided in the ``pixSortableBehaviorBundle`` ]); -In order to add new routes for these actions we are also adding the following method - -.. code-block:: php +In order to add new routes for these actions we are also adding the following method:: 'Value'`; useful when simple getter is not enough). Notice: works with string-like types (string, text, html) -- ``associated_property`` (o): property path to retrieve the "string" representation of the collection element, or a closure with the element as argument and return a string. +- ``associated_property`` (o): property path to retrieve the "string" + representation of the collection element, or a closure with the element + as argument and return a string. - ``identifier`` (o): if set to true a link appears on the value to edit the element Available types and associated options @@ -192,10 +195,10 @@ Available types and associated options | | hide_protocol | Hide http:// or https:// (default: false) | +-----------+----------------+-----------------------------------------------------------------------+ -If you have the SonataDoctrineORMAdminBundle installed, you have access to more field types, see `SonataDoctrineORMAdminBundle Documentation `_. +If you have the SonataDoctrineORMAdminBundle installed, you have access +to more field types, see `SonataDoctrineORMAdminBundle Documentation`_. .. note:: - It is better to prefer non negative notions when possible for boolean values so use the ``inverse`` option if you really cannot find a good enough antonym for the name you have. @@ -233,7 +236,7 @@ the ``datagridValues`` array property. All three keys ``_page``, ``_sort_order`` .. code-block:: php add('description', 'text', [ + ->add('description', TextType::class, [ 'header_style' => 'width: 35%', 'collapse' => [ 'height' => 40, // height in px @@ -595,9 +600,7 @@ If you want to customise the `collapse` option, you can also give an array to ov ]) // ... -If you want to show only the `label_icon`: - -.. code-block:: php +If you want to show only the `label_icon`:: // ... ->add('upvotes', null, [ @@ -606,7 +609,7 @@ If you want to show only the `label_icon`: ]) // ... -.. _`issues on GitHub`: https://github.com/sonata-project/SonataAdminBundle/issues/1519 +`issues on GitHub`_ Mosaic view button ------------------ @@ -619,8 +622,8 @@ You have the possibility to show/hide mosaic view button. # for hide mosaic view button on all screen using `false` show_mosaic_button: true -You can show/hide mosaic view button using admin service configuration. You need to add option ``show_mosaic_button`` -in your admin services: +You can show/hide mosaic view button using admin service configuration. +You need to add option ``show_mosaic_button`` in your admin services: .. code-block:: yaml @@ -640,6 +643,9 @@ Checkbox range selection ------------------------ .. tip:: - You can check / uncheck a range of checkboxes by clicking a first one, then a second one with shift + click. + +.. _`issues on GitHub`: https://github.com/sonata-project/SonataAdminBundle/issues/1519 +.. _`SonataDoctrineORMAdminBundle Documentation`: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/list_field_definition.html +.. _`here`: https://github.com/sonata-project/SonataCoreBundle/tree/master/Form/Type \ No newline at end of file diff --git a/docs/reference/action_show.rst b/docs/reference/action_show.rst index 6ffd999d28..c609ed28cb 100644 --- a/docs/reference/action_show.rst +++ b/docs/reference/action_show.rst @@ -2,13 +2,17 @@ The Show action =============== .. note:: - This document is a stub representing a new work in progress. If you're reading this you can help contribute, **no matter what your experience level with Sonata is**. Check out the `issues on GitHub`_ for more information about how to get involved. This document will cover the Show action and related configuration options. +.. 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. + Basic configuration ------------------- @@ -27,9 +31,12 @@ Group options When adding a group to your show page, you may specify some options for the group itself. - ``collapsed``: unused at the moment -- ``class``: the class for your group in the admin; by default, the value is set to ``col-md-12``. -- ``fields``: the fields in your group (you should NOT override this unless you know what you're doing). -- ``box_class``: the class for your group box in the admin; by default, the value is set to ``box box-primary``. +- ``class``: the class for your group in the admin; by default, the value + is set to ``col-md-12``. +- ``fields``: the fields in your group (you should NOT override this unless + you know what you're doing). +- ``box_class``: the class for your group box in the admin; by default, + the value is set to ``box box-primary``. - ``description``: to complete - ``translation_domain``: to complete @@ -38,7 +45,7 @@ To specify options, do as follow: .. code-block:: php add('name', null, ['label' => 'UserName']); @@ -161,7 +161,7 @@ The first thing you need to do is define it in app/config/config/yml: Once you have defined this, Sonata Admin looks for it in the following location: -``src/AppBundle/Resources/views/Admin/Display_Client.html.twig`` +``templates/Admin/Display_Client.html.twig`` Now that you have told Sonata Admin where to find the template, it is time to put one in there. diff --git a/docs/reference/advanced_configuration.rst b/docs/reference/advanced_configuration.rst index d856752b57..e9248bd476 100644 --- a/docs/reference/advanced_configuration.rst +++ b/docs/reference/advanced_configuration.rst @@ -1,10 +1,16 @@ Advanced configuration ====================== +.. 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. + Service Configuration --------------------- -When you create a new Admin service you can configure its dependencies, the services which are injected by default are: +When you create a new Admin service you can configure its dependencies, +the services which are injected by default are: ========================= ============================================= Dependencies Service Id @@ -37,7 +43,7 @@ You have 2 ways of defining the dependencies inside ``services.xml``: .. code-block:: xml - + - AppBundle\Entity\Project + App\Entity\Project @@ -56,12 +62,12 @@ You have 2 ways of defining the dependencies inside ``services.xml``: .. code-block:: yaml app.admin.project: - class: AppBundle\Admin\ProjectAdmin + class: App\Admin\ProjectAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Project", label: "Project", label_translator_strategy: "sonata.admin.label.strategy.native", route_builder: "sonata.admin.route.path_info" } arguments: - ~ - - AppBundle\Entity\Project + - App\Entity\Project - ~ public: true @@ -71,7 +77,7 @@ You have 2 ways of defining the dependencies inside ``services.xml``: .. code-block:: xml - + - AppBundle\Entity\Project + App\Entity\Project @@ -96,12 +102,12 @@ You have 2 ways of defining the dependencies inside ``services.xml``: .. code-block:: yaml app.admin.project: - class: AppBundle\Admin\ProjectAdmin + class: App\Admin\ProjectAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Project", label: "Project" } arguments: - ~ - - AppBundle\Entity\Project + - App\Entity\Project - ~ calls: - [ setLabelTranslatorStrategy, [ "@sonata.admin.label.strategy.native" ]] @@ -115,7 +121,7 @@ application's config file: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml admins: sonata_admin: sonata.order.admin.order: # id of the admin service this setting is for @@ -133,7 +139,7 @@ To create your own RouteBuilder create the PHP class and register it as a servic .. code-block:: php + @@ -177,7 +183,7 @@ To create your own RouteBuilder create the PHP class and register it as a servic services: app.admin.entity_route_builder: - class: AppBundle\Route\EntityRouterBuilder + class: App\Route\EntityRouterBuilder arguments: - "@sonata.admin.audit.manager" @@ -192,25 +198,24 @@ Lets consider a base class named `Person` and its subclasses `Student` and `Teac .. code-block:: xml - + - AppBundle\Entity\Person + App\Entity\Person - AppBundle\Entity\Student - AppBundle\Entity\Teacher + App\Entity\Student + App\Entity\Teacher -You will just need to change the way forms are configured in order to take into account these new subclasses: - -.. code-block:: php +You will just need to change the way forms are configured in order to +take into account these new subclasses:: true` attribute: - -.. code-block:: php +the `'dropdown' => true` attribute:: setExtra('translation_domain', 'My domain'); Filter parameters ^^^^^^^^^^^^^^^^^ -You can add or override filter parameters to the Tab Menu: - -.. code-block:: php +You can add or override filter parameters to the Tab Menu:: `` tag. +You can disable ``html``, ``body`` and ``sidebar`` elements stretching. +These containers are forced to be full height by default. If you use +custom layout or just don't need such behavior, add ``no-stretch`` class +to the ```` tag. For example: .. code-block:: html+jinja - {# src/AppBundle/Resources/views/standard_layout.html.twig #} + {# templates/standard_layout.html.twig #} {% block html_attributes %}class="no-js no-stretch"{% endblock %} Custom Action Access Management ------------------------------- -You can customize the access system inside the CRUDController by adding some entries inside the `$accessMapping` array in the linked Admin. +You can customize the access system inside the CRUDController by adding +some entries inside the `$accessMapping` array in the linked Admin. .. code-block:: php + - AppBundle\Entity\Post + App\Entity\Post @@ -83,19 +88,19 @@ your ``Admin`` services. This is done using a ``call`` to the matching ``setter` services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" } arguments: - ~ - - AppBundle\Entity\Post + - App\Entity\Post - ~ calls: - [ setLabelTranslatorStrategy, ["@sonata.admin.label.strategy.underscore"]] public: true -Here, we declare the same ``Admin`` service as in the :doc:`../getting_started/creating_an_admin` chapter, but using a -different label translator strategy, replacing the default one. Notice that +Here, we declare the same ``Admin`` service as in the :doc:`../getting_started/creating_an_admin` +chapter, but using a different label translator strategy, replacing the default one. Notice that ``sonata.admin.label.strategy.underscore`` is a service provided by ``SonataAdminBundle``, but you could just as easily use a service of your own. @@ -119,19 +124,19 @@ the Dependency Injection Container (DIC). This is particularly useful if you decide to extend the ``CRUDController`` to add new actions or change the behavior of existing ones. You can specify which controller to use when declaring the ``Admin`` service by passing it as the 3rd argument. For example -to set the controller to ``AppBundle:PostAdmin``: +to set the controller to ``App:PostAdmin``: .. configuration-block:: .. code-block:: xml - + - AppBundle\Entity\Post - AppBundle:PostAdmin + App\Entity\Post + App:PostAdmin - AppBundle + App @@ -139,15 +144,15 @@ to set the controller to ``AppBundle:PostAdmin``: services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" } arguments: - ~ - - AppBundle\Entity\Post - - AppBundle:PostAdmin + - App\Entity\Post + - App\Controller\PostAdminController calls: - - [ setTranslationDomain, [AppBundle]] + - [ setTranslationDomain, [App]] public: true When extending ``CRUDController``, remember that the ``Admin`` class already has @@ -173,20 +178,21 @@ Your ``Admin`` class defines which of your model's fields will be available in e action defined in your ``CRUDController``. So, for each action, a list of field mappings is generated. These lists are implemented using the ``FieldDescriptionCollection`` class which stores instances of ``FieldDescriptionInterface``. Picking up on our previous -``PostAdmin`` class example: - -.. code-block:: php +``PostAdmin`` class example:: add('title', 'text', [ + ->add('title', TextType:class, [ 'label' => 'Post Title' ]) - ->add('author', 'entity', [ - 'class' => 'AppBundle\Entity\User' + ->add('author', EntityType::class, [ + 'class' => User::class ]) // if no type is specified, SonataAdminBundle tries to guess it diff --git a/docs/reference/batch_actions.rst b/docs/reference/batch_actions.rst index 210467fa8d..19c50150b1 100644 --- a/docs/reference/batch_actions.rst +++ b/docs/reference/batch_actions.rst @@ -2,15 +2,22 @@ Batch actions ============= Batch actions are actions triggered on a set of selected objects. By default, -Admins have a ``delete`` action which allows you to remove several entries at once. +Admins have a ``delete`` action which allows you to remove several entries +at once. + +.. 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. Defining new actions -------------------- To create a new custom batch action which appears in the list view follow these steps: -Override ``configureBatchActions()`` in your ``Admin`` class to define the new batch actions -by adding them to the ``$actions`` array. Each key represent a batch action and could contain these settings: +Override ``configureBatchActions()`` in your ``Admin`` class to define +the new batch actions by adding them to the ``$actions`` array. +Each key represent a batch action and could contain these settings: - **label**: The name to use when offering this option to users, should be passed through the translator (default: the label is generated via the labelTranslatorStrategy) @@ -61,9 +68,9 @@ granularity), the passed query is ``null``. .. code-block:: php {% endblock %} - -And add this: - -.. code-block:: php - - IsRelevant`` method -(e.g. ``batchActionMergeIsRelevant``) in your ``CRUDController`` class. This check is performed -before the user is asked for confirmation, to make sure there is actually something to confirm. +By default, batch actions are not executed if no object was selected, and +the user is notified of this lack of selection. If your custom batch action +needs more complex logic to determine if an action can be performed or not, +just define a ``batchActionIsRelevant`` method (e.g. ``batchActionMergeIsRelevant``) +in your ``CRUDController`` class. This check is performed before the user is asked for confirmation, +to make sure there is actually something to confirm. This method may return three different values: @@ -190,9 +182,9 @@ This method may return three different values: .. code-block:: php setParameter('foo', $bar); } - -.. _Symfony bundle overriding mechanism: http://symfony.com/doc/current/cookbook/bundles/inheritance.html diff --git a/docs/reference/breadcrumbs.rst b/docs/reference/breadcrumbs.rst index 66954e9983..9c09bb0d6b 100644 --- a/docs/reference/breadcrumbs.rst +++ b/docs/reference/breadcrumbs.rst @@ -1,15 +1,18 @@ The breadcrumbs builder ======================= +.. 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 ``sonata.admin.breadcrumbs_builder`` service is used in the layout of every page to compute the underlying data for two breadcrumbs: * one as text, appearing in the ``title`` tag of the document's ``head`` tag; * the other as html, visible as an horizontal bar at the top of the page. -Getting the breadcrumbs for a given action of a given admin is done like this: - -.. code-block:: php +Getting the breadcrumbs for a given action of a given admin is done like this:: get('sonata.admin.breadcrumbs_builder')->getBreadcrumbs($admin, $action); @@ -20,7 +23,7 @@ Configuration .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: breadcrumbs: diff --git a/docs/reference/child_admin.rst b/docs/reference/child_admin.rst index c34080e08d..16450609c8 100644 --- a/docs/reference/child_admin.rst +++ b/docs/reference/child_admin.rst @@ -1,18 +1,25 @@ Create child admins ------------------- -Let us say you have a ``PlaylistAdmin`` and a ``VideoAdmin``. You can optionally declare the ``VideoAdmin`` -to be a child of the ``PlaylistAdmin``. This will create new routes like, for example, ``/playlist/{id}/video/list``, +.. 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. + +Let us say you have a ``PlaylistAdmin`` and a ``VideoAdmin``. You can +optionally declare the ``VideoAdmin`` to be a child of the ``PlaylistAdmin``. +This will create new routes like, for example, ``/playlist/{id}/video/list``, where the videos will automatically be filtered by post. -To do this, you first need to call the ``addChild`` method in your ``PlaylistAdmin`` service configuration: +To do this, you first need to call the ``addChild`` method in your ``PlaylistAdmin`` +service configuration: .. configuration-block:: .. code-block:: xml - + @@ -26,7 +33,7 @@ Then, you have to set the VideoAdmin ``parentAssociationMapping`` attribute to ` [] diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index db5a90b163..d9b6d6249b 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -16,7 +16,7 @@ Configuration options .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: security: diff --git a/docs/reference/console.rst b/docs/reference/console.rst index 466bcf15ed..2500f83651 100644 --- a/docs/reference/console.rst +++ b/docs/reference/console.rst @@ -20,7 +20,7 @@ Usage example: .. code-block:: bash - $ php app/console cache:create-cache-class + $ bin/console cache:create-cache-class sonata:admin:generate --------------------- @@ -40,7 +40,7 @@ The command require the SensioGeneratorBundle_ to work. If you don't already hav =============== =============================================================================================================================== Options Description =============== =============================================================================================================================== - **bundle** the bundle name (the default value is determined by the given model class, e.g. "AppBundle" or "YourNSFooBundle") + **bundle** the bundle name (the default value is determined by the given model class, e.g. "App" or "YourNSFooBundle") **admin** the admin class basename (by default this adds "Admin" to the model class name, e.g. "BarAdmin") **controller** the controller class basename (by default this adds "AdminController" to the model class name, e.g. "BarAdminController") **manager** the model manager type (by default this is the first registered model manager type, e.g. "orm") @@ -52,7 +52,7 @@ Usage example: .. code-block:: bash - $ php app/console sonata:admin:generate AppBundle/Entity/Foo + $ bin/console sonata:admin:generate App/Entity/Foo sonata:admin:list ----------------- @@ -66,7 +66,7 @@ Usage example: .. code-block:: bash - $ php app/console sonata:admin:list + $ bin/console sonata:admin:list .. figure:: ../images/console_admin_list.png @@ -86,7 +86,7 @@ Usage example: .. code-block:: bash - $ php app/console sonata:admin:explain sonata.news.admin.post + $ bin/console sonata:admin:explain sonata.news.admin.post .. figure:: ../images/console_admin_explain.png :align: center @@ -108,7 +108,7 @@ Usage example: .. code-block:: bash - $ php app/console sonata:admin:setup-acl + $ bin/console sonata:admin:setup-acl sonata:admin:generate-object-acl -------------------------------- diff --git a/docs/reference/dashboard.rst b/docs/reference/dashboard.rst index d17c7feda6..9122a0869d 100644 --- a/docs/reference/dashboard.rst +++ b/docs/reference/dashboard.rst @@ -1,6 +1,11 @@ Dashboard ========= +.. 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 Dashboard is the main landing page. By default it lists your mapped models, as defined by your ``Admin`` services. This is useful to help you start using ``SonataAdminBundle`` right away, but there is much more that you can do to take @@ -9,13 +14,13 @@ advantage of the Dashboard. The Dashboard is, by default, available at ``/admin/dashboard``, which is handled by the ``SonataAdminBundle:Core:dashboard`` controller action. The default view file for this action is ``@SonataAdmin/Core/dashboard.html.twig``, but you can change -this in your ``config.yml``: +this in your admin configuration: .. configuration-block:: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: templates: @@ -65,12 +70,12 @@ services: .. code-block:: xml - + - AppBundle\Entity\Post + App\Entity\Post @@ -78,7 +83,7 @@ services: services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - name: sonata.admin manager_type: orm @@ -86,7 +91,7 @@ services: label: "Post" arguments: - ~ - - AppBundle\Entity\Post + - App\Entity\Post - ~ public: true @@ -97,12 +102,12 @@ service belongs to the ``Content`` group. .. code-block:: xml - + + label="app.admin.model.post" label_catalogue="App" /> - AppBundle\Entity\Post + App\Entity\Post @@ -110,19 +115,19 @@ service belongs to the ``Content`` group. services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - name: sonata.admin manager_type: orm group: "app.admin.group.content" label: "app.admin.model.post" - label_catalogue: "AppBundle" + label_catalogue: "App" arguments: - ~ - - AppBundle\Entity\Post + - App\Entity\Post - ~ -In this example, the labels are translated by ``AppBundle``, using the given +In this example, the labels are translated by ``App``, using the given ``label_catalogue``. So, you can use the above examples to support multiple languages in your project. @@ -142,14 +147,14 @@ declarations. .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: groups: app.admin.group.content: label: app.admin.group.content - label_catalogue: AppBundle + label_catalogue: App items: - app.admin.post @@ -168,7 +173,7 @@ declarations. either leave out that key or use the ``~`` value for that option. This configuration specifies that the ``app.admin.group.content`` group uses the -``app.admin.group.content`` label, which is translated using the ``AppBundle`` +``app.admin.group.content`` label, which is translated using the ``App`` translation catalogue (the same label and translation configuration that we declared previously, in the service definition example). @@ -208,7 +213,7 @@ a text block and RSS feed block on the right. The configuration for this scenari .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -255,7 +260,7 @@ suit this scenario. .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -347,7 +352,7 @@ On ``top`` and ``bottom`` positions, you can also specify an optional ``class`` .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: dashboard: @@ -365,12 +370,10 @@ Configuring what actions are available for each item on the dashboard By default. A "list" and a "create" option are available for each item on the dashboard. If you created a custom action and want to display it along the other two on the dashboard, you can do so by overriding the -``getDashboardActions()`` method of your admin class: - -.. code-block:: php +``getDashboardActions()`` method of your admin class:: add('status', 'choice', [ 'choices' => [ @@ -77,7 +77,7 @@ Choice 'prog' => 'In progress', 'done' => 'Done' ], - 'catalogue' => 'AppBundle' + 'catalogue' => 'App' ]) ; } diff --git a/docs/reference/form_help_message.rst b/docs/reference/form_help_message.rst index 4cef680a32..f213497fcc 100644 --- a/docs/reference/form_help_message.rst +++ b/docs/reference/form_help_message.rst @@ -1,10 +1,18 @@ Form Help Messages and Descriptions =================================== +.. 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. + Help Messages ------------- -Help messages are short notes that are rendered together with form fields. They are generally used to show additional information so the user can complete the form element faster and more accurately. The text is not escaped, so HTML can be used. +Help messages are short notes that are rendered together with form fields. +They are generally used to show additional information so the user can complete +the form element faster and more accurately. The text is not escaped, +so HTML can be used. Example ^^^^^^^ @@ -12,7 +20,7 @@ Example .. code-block:: php > + $ bin/console sonata:admin:explain <> for example if your Admin is called sonata.admin.foo you would run .. code-block:: bash - $ php app/console sonata:admin:explain app.admin.foo + $ bin/console sonata:admin:explain app.admin.foo Sonata internally checks for the existence of a route before linking to it. As a result, removing a route will prevent links to that action from appearing in the administrative interface. For example, @@ -263,12 +263,10 @@ removing the 'create' route will prevent any links to "Add new" from appearing. Removing a single route ^^^^^^^^^^^^^^^^^^^^^^^ -Any single registered route can be easily removed by name: - -.. code-block:: php +Any single registered route can be easily removed by name:: generateUrl('create')`` somewhere, the generated URL looks like this: ``/admin/module/create?context=default`` +If you then call ``$admin->generateUrl('create')`` somewhere, the generated +URL looks like this: ``/admin/module/create?context=default`` Changing the default route in a List Action ------------------------------------------- Usually the identifier column of a list action links to the edit screen. To change the list action's links to point to a different action, set the ``route`` option in your call to -``ListMapper::addIdentifier()``. For example, to link to show instead of edit: - -.. code-block:: php +``ListMapper::addIdentifier()``. For example, to link to show instead of edit:: add('plainPassword', 'text') ->end() ->with('Groups') - ->add('groups', 'sonata_type_model', ['required' => false]) + ->add('groups', ModelType::class, ['required' => false]) ->end() ->with('Management') - ->add('roles', 'sonata_security_roles', ['multiple' => true]) + ->add('roles', SecurityRolesType::class, ['multiple' => true]) ->add('locked', null, ['required' => false]) ->add('expired', null, ['required' => false]) ->add('enabled', null, ['required' => false]) diff --git a/docs/reference/security.rst b/docs/reference/security.rst index 9c0281efad..bb55be6dbe 100644 --- a/docs/reference/security.rst +++ b/docs/reference/security.rst @@ -1,6 +1,11 @@ Security ======== +.. 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. + User management --------------- @@ -59,7 +64,7 @@ Using roles: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: security: @@ -74,7 +79,7 @@ Using ACL: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: security: @@ -132,14 +137,14 @@ service), Sonata will check if the user has the ``ROLE_APP_ADMIN_FOO_EDIT`` or ` .. note:: - Declaring the same admin as `AppBundle\Admin\FooAdmin` results in + Declaring the same admin as `App\Admin\FooAdmin` results in ``ROLE_APPBUNDLE\ADMIN\FOOADMIN_EDIT`` and ``ROLE_APPBUNDLE\ADMIN\FOOADMIN_ALL``! The role name will be based on the name of your admin service. ======================== ====================================================== app.admin.foo ROLE_APP_ADMIN_FOO_{PERMISSION} my.blog.admin.foo_bar ROLE_MY_BLOG_ADMIN_FOO_BAR_{PERMISSION} -AppBundle\Admin\FooAdmin ROLE_APPBUNDLE\ADMIN\FOOADMIN_{PERMISSION} +App\Admin\FooAdmin ROLE_APPBUNDLE\ADMIN\FOOADMIN_{PERMISSION} ======================== ====================================================== .. note:: @@ -152,7 +157,7 @@ So our ``security.yml`` file may look something like this: .. code-block:: yaml - # app/config/security.yml + # config/packages/security.yaml security: # ... @@ -189,9 +194,7 @@ in the Symfony documentation. Usage ~~~~~ -You can now test if a user is authorized from an Admin class: - -.. code-block:: php +You can now test if a user is authorized from an Admin class:: if ($this->hasAccess('list')) { // ... @@ -239,7 +242,7 @@ Then declare your handler as a service: .. code-block:: xml - + ROLE_SUPER_ADMIN @@ -252,7 +255,7 @@ And specify it as Sonata security handler on your configuration: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: security: @@ -284,9 +287,9 @@ User class (in a custom UserBundle). Do it as follows: .. code-block:: php = 3 services: security.acl.permission.map: class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap - # optionally use a custom MaskBuilder - #sonata.admin.security.mask.builder: - # class: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder - - # Symfony < 3 + # optionally use a custom MaskBuilder parameters: - security.acl.permission.map.class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap + sonata.admin.security.mask.builder.class: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder - # optionally use a custom MaskBuilder - #sonata.admin.security.mask.builder.class: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder -In ``app/config/security.yml``: +In ``config/packages/security.yaml``: .. configuration-block:: .. code-block:: yaml - # app/config/security.yml + # config/packages/security.yaml security: providers: @@ -416,13 +412,13 @@ In ``app/config/security.yml``: acl: connection: default -- Install the ACL tables ``php app/console init:acl`` +- Install the ACL tables ``bin/console init:acl`` - Create a new root user: .. code-block:: bash - $ php app/console fos:user:create --super-admin + $ bin/console fos:user:create --super-admin Please choose a username:root Please choose an email:root@domain.com Please choose a password:root @@ -432,7 +428,7 @@ If you have Admin classes, you can install or update the related CRUD ACL rules: .. code-block:: bash - $ php app/console sonata:admin:setup-acl + $ bin/console sonata:admin:setup-acl Starting ACL AdminBundle configuration > install ACL for sonata.media.admin.media - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_GUEST, permissions: ["VIEW","LIST"] @@ -446,7 +442,7 @@ object of an admin: .. code-block:: bash - $ php app/console sonata:admin:generate-object-acl + $ bin/console sonata:admin:generate-object-acl Optionally, you can specify an object owner, and step through each admin. See the help of the command for more information. @@ -552,9 +548,9 @@ because for example you want to restrict access using extra rules: .. code-block:: php + - + - + @@ -628,7 +624,7 @@ because for example you want to restrict access using extra rules: .. code-block:: yaml - # app/config/security.yml + # config/packages/security.yaml security: access_decision_manager: @@ -657,7 +653,7 @@ Usage ~~~~~ Every time you create a new ``Admin`` class, you should start with the command -``php app/console sonata:admin:setup-acl`` so the ACL database will be updated +``bin/console sonata:admin:setup-acl`` so the ACL database will be updated with the latest roles and permissions. In the templates, or in your code, you can use the Admin method ``hasAccess()``: @@ -775,7 +771,7 @@ service to use when retrieving your users. .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: security: diff --git a/docs/reference/templates.rst b/docs/reference/templates.rst index 2888d513f7..5897a84aa0 100644 --- a/docs/reference/templates.rst +++ b/docs/reference/templates.rst @@ -1,6 +1,11 @@ Templates ========= +.. 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. + ``SonataAdminBundle`` comes with a significant amount of ``twig`` files used to display the different parts of each ``Admin`` action's page. If you read the ``Templates`` part of the :doc:`architecture` section of this guide, you should know by now how these are organized in @@ -108,7 +113,7 @@ You can specify your templates in the config.yml file, like so: .. code-block:: yaml - # app/config/config.yml + # config/packages/sonata_admin.yaml sonata_admin: templates: @@ -159,10 +164,10 @@ specify the templates to use in the ``Admin`` service definition: .. code-block:: xml - + - AppBundle\Entity\Post + App\Entity\Post edit @@ -174,12 +179,12 @@ specify the templates to use in the ``Admin`` service definition: services: app.admin.post: - class: AppBundle\Admin\PostAdmin + class: App\Admin\PostAdmin tags: - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" } arguments: - ~ - - AppBundle\Entity\Post + - App\Entity\Post - ~ calls: - [ setTemplate, [edit, "@App/PostAdmin/edit.html.twig"]] @@ -215,4 +220,4 @@ function to access the templates of the current Admin, or the {% block field %} {# ... #} - {% endblock %} \ No newline at end of file + {% endblock %} diff --git a/docs/reference/translation.rst b/docs/reference/translation.rst index 0f1ae22c5a..55fb2319ba 100644 --- a/docs/reference/translation.rst +++ b/docs/reference/translation.rst @@ -1,6 +1,11 @@ Translation =========== +.. 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. + There are two main catalogue names in an Admin class: * ``SonataAdminBundle``: this catalogue is used to translate shared messages @@ -76,29 +81,30 @@ Overriding the translation domain is of particular use when using :doc:`extensions`, where the extension and the translations would be defined in one bundle, but implemented in many different Admin instances. -Setting the translation domain on an individual field: +Setting the translation domain on an individual field:: -.. code-block:: php + use Symfony\Component\Form\Extension\Core\Type\CheckboxType; $formMapper ->with('form.my_group') - ->add('publishable', 'checkbox', [], [ + ->add('publishable', CheckboxType::class, [], [ 'translation_domain' => 'MyTranslationDomain', ]) ->end() ; The following example sets the default translation domain on a form group and -over-rides that setting for one of the fields: +over-rides that setting for one of the fields:: -.. code-block:: php + use Symfony\Component\Form\Extension\Core\Type\CheckboxType; + use Symfony\Component\Form\Extension\Core\Type\DateType; $formMapper ->with('form.my_group', ['translation_domain' => 'MyDomain']) - ->add('publishable', 'checkbox', [], [ + ->add('publishable', CheckboxType::class, [], [ 'translation_domain' => 'AnotherDomain', ]) - ->add('start_date', 'date', [], []) + ->add('start_date', DateType::class, [], []) ->end() ; @@ -109,9 +115,7 @@ Setting the label name ^^^^^^^^^^^^^^^^^^^^^^ By default, the label is set to a sanitized version of the field name. A custom -label can be defined as the third argument of the ``add`` method: - -.. code-block:: php +label can be defined as the third argument of the ``add`` method:: class PageAdmin extends AbstractAdmin { @@ -161,7 +165,7 @@ the Container: .. code-block:: xml - + - AppBundle\Entity\Project + App\Entity\Project diff --git a/docs/reference/troubleshooting.rst b/docs/reference/troubleshooting.rst index 45f1875b66..3e5ea1397d 100644 --- a/docs/reference/troubleshooting.rst +++ b/docs/reference/troubleshooting.rst @@ -1,18 +1,21 @@ Troubleshooting =============== +.. 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 toString method ------------------- Sometimes the bundle needs to display your model objects, in order to do it, objects are converted to string by using the `__toString`_ magic method. Take care to never return anything else than a string in this method. -For example, if your method looks like that : - -.. code-block:: php +For example, if your method looks like that::