Formulation of the problem
In the
first part of the article, we created a button in the List View line of letters that takes us to the form for creating an answer. However, at least two important questions remained unresolved:
- automatic linking of the response to the letter
- checking the user's rights to create a response
Automatic linking of the response to the letter
There are at least three ways to automatically link a response to a letter at the SonataAdminBundle level:
1)
After creating the form before saving the response entity to the database (for this, you can use the prePersist method of the Admin object). The main feature of this method is the “opacity” of attaching one entity to another for a user working with a form, which can serve both as a strength and a disadvantage depending on the goals.
2)
While creating the form by setting the value of the required field using formBuilder. This approach can lead to undesirable cluttering of the admin class with a code that implements the logic for generating the default field value.
3)
Before creating the form. To do this, you can use the approach proposed, for example, in
[1] , which is to override the parent method of the getNewInstance Admin class. An alternative to this solution may be inheritance from a CRUD controller (the inheritance process is described in detail in
[2] ) and the definition of a hook operation
[3] preCreate in it. One of the advantages of such an alternative is the “full-time” ability to return a full-featured \ Symfony \ Component \ HttpFoundation \ Response () object if necessary.
Which method to use depends on the problem to be solved, we consider the third one using inheritance from a CRUD controller, since, in our opinion, it is the least obvious from the point of view of implementation and the most flexible from the point of view of architecture.
First of all, add the third argument to the admin.getRouteGenerator.generateUrl () function. This should be an array of request parameters and we will add to it the letter identifier, from the line of which we create the answer. To do this, use the admin.getUrlsafeIdentifier () function.
{# src/AppBundle/Resources/views/CRUD/list__action_create_other_admin.html.twig #} <a href="{{ admin.getRouteGenerator.generateUrl(template_variables.otherAdmin, 'create', {'incoming_id': admin.getUrlsafeIdentifier(object)}) }}" class="btn btn-sm btn-default edit_link" title=" "> <i class="fa fa-plus"></i> </a>
Thus, clicking on a link will allow you to access the createAction of the CRUD controller, passing the message identifier of the message you need as the query parameter incoming_id_. Now the task is reduced to receiving the letter object by identifier and attaching an answer to it. We will solve it by defining the default default preCreate method, which can return an \ Symfony \ Component \ HttpFoundation \ Response () object or return nothing just by modifying $ object.
namespace Application\Sonata\AdminBundle\Controller; use AppBundle\Entity\Response; use Sonata\AdminBundle\Controller\CRUDController as BaseController; use Symfony\Component\HttpFoundation\Request; class CRUDController extends BaseController { public function preCreate(Request $request, $object) {
The main task set at the beginning of the article, namely: the creation of the essence of the answer from the ListView of letters is solved. Consider the question of checking the user's rights to create a response
')
Checking user rights to create a response
The direct question of setting the user's rights to create an answer lies outside the topic of this article and can be solved on the basis of the detailed information set out in
[4] . We will consider the question of showing or hiding the “Add Answer” button in the ListView of LETTERs, depending on whether the user has or does not have permission to create an ANSWER. To do this, we will again have to make changes to the list__action_create_other_admin.html.twig file.
{# src/AppBundle/Resources/views/CRUD/list__action_create_other_admin.html.twig #} {% if template_variables.otherAdmin.securityHandler.isGranted(template_variables.otherAdmin, 'CREATE', template_variables.otherAdmin) and template_variables.otherAdmin.hasRoute('create') %} <a href="{{ admin.getRouteGenerator.generateUrl(template_variables.otherAdmin, 'create', {'incoming_id': admin.getUrlsafeIdentifier(object)}) }}" class="btn btn-sm btn-default edit_link" title=" "> <i class="fa fa-plus"></i> </a> {% endif %}
At the same time, we check the possibility of creating a response not only at the level of the security system, but also at the level of the routing system.
Summary
The article presents a solution to the problem of creating in SonataAdminBundle some entity from the ListView of the second entity associated with the first one, taking into account whether the user has or does not have the appropriate rights.
Links to used resources
- Populate resp. set default values ​​on form resp. object or instance in SonataAdminBundle
- CREATING A CUSTOM ADMIN ACTION
- Hook operations
- Security