tell me pls. Why if I specify a action button template, the button is displayed regardless of the access rights?
$listMapper
->add('_action', 'actions', [
'actions' => [
//displayed depending on the access rights
'edit' => [],
//displayed regardless of access rights
'delete' => [
'template' => '#App/list__action_delete.html.twig',
],
]
]);
And how to specify a template so that the button is displayed depending on the access rights?
Probable reason is that you've forgot to add access check into your custom template.
If you look into build-in Sonata template you'll see that actual access check is done in template themselves not in outer code. So just copy those check from original template into yours.
Example:
{% if admin.hasAccess('delete', object) and admin.hasRoute('delete') %}
{# --- Your custom button view here --- #}
{% endif %}
Related
I have a show page and I want to add a custom value.
I have tried doing what I did in other actions which is to add an array to the
third parameter with the data key like so:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
In the configureListFields action, this works. I have injected custom values with the data attribute.
But still I am not able to access key example in the show.html.twig file.
It gives me this error
Variable "example" does not exist.
What should I do to access this custom variable in the twig file ?
Try
{{ elements.elements.example.options.data }}
in your twig template
I used this solution. In the configureShowFields() method of an Admin class:
$showMapper
->with('Tab Name')
->add(
'any_name',
null,
[
'template' => 'Admin/Custom/any_name_show_template.html.twig',
'customData' => $this->someRepository->getSomeEntityBy($field),
'anotherCustomData' => $this->someService->getSomeDataBy($value),
]
)
;
In the custom template, you can access custom data by field_description.options.<customFieldName>, so for provided example data accessors would be {{ field_description.options.customData }} and {{ field_description.options.anotherCustomData }}
For the shorter field name in the Twig template, you can do like this:
{% set customData = field_description.options.customData %}
and access the custom data like {{ customData }}
Hope this helps and saves time.
I would like to set a special div surrounding a bunch of my fields. For that I want to add something to the form builder that I could detect in my form_theme, and set the div when it's there.
I tried to add
->add('field', new myCustomType(), array('inherit_data' => true, "label" => false, "required" => false, 'attr' => array("test" => "aaa")))
to the form builder, setting an custom attr, it's actually rendered in the html as an attribute... But I'm unable to detect it in the form theme.
{{ block('widget_container_attributes') }}
Only gives the widget attributes, and
{{ block('row_container_attributes') }}
doesn't work. I actually have a hard time finding any source online about what variables are available in the blocks of the form theme and how to use them (it was already difficult to know how to call blocks).
I looked for some more information on the official site, here mostly but without any success...
Thanks ahead for any help !
If you put it in your form builder, then you might as well permanently set in your template. If there is some logic required to set the data, then that belongs in your controller anyway, so just put it there to start with.
Controller:
public function someAction()
{
// ....
return $this->render('some_twig_template.twig.html', array(
'attr' => array("test" => "aaa")
);
}
Then in your twig template
{{ dump(attr) }}
{{ dump(attr.test) }}
EDIT:
To render in your template every time, you can set a class on the rendered field directly:
{{ form_label(form.field, 'My label', { 'label_attr': {'class': 'js-hidden-row'} }) }}
{{ form_widget(form.field, { 'attr': {'class': 'js-hidden-row'} }) }}
Then in my javascript you can hide with some simple jQuery:
<script>
jQuery(document).ready(function() {
$('.js-hidden-row').hide();
});
</script>
I use Sonata Admin 3.13 with Symfony 3.1 and want to display the uploaded image in the listview. I have a PaintingAdmin with the following ListFields:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name', null, ['label' => 'Name'])
->add('category', null, ['label' => 'Kategorie'])
->add('size', null, ['label' => 'Größe'])
->add('imageFilename', null, [
'template' => 'sonata:imagepreview.html.twig',
'label' => 'Bild'
]);
;
}
And a template at app/Resources/views/sonata/imagepreview.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{% if object.imageFilename != null %}
<img src="{{ asset('uploads/images/' ~ object.imageFilename) }}" class="img-responsive" />
{% else %}
<div class="warn">Kein Bild</div>
{% endif %}
</div>
{% endblock %}
but the template is completly ignored, it shows only the value imageFilename. Everything else works fine (i.e. the label is shown as Bild)
You need to tell Sonata that you are using a custom template.
You can do it in your admin service declaration:
librinfo_crm.admin.organism:
class: Librinfo\CRMBundle\Admin\OrganismAdmin
arguments: [~, Librinfo\CRMBundle\Entity\Organism, LibrinfoCRMBundle:OrganismAdmin]
tags:
- name: sonata.admin
manager_type: orm
group: Customers Relationship Management
label: librinfo.crm.organism_admin.label
label_translator_strategy: blast_core.label.strategy.librinfo
calls:
- [ setTemplate, [list, LibrinfoCRMBundle:OrganismAdmin:list.html.twig]]
or you can override the $templates array() of your admin class.
If you want to use 'classic' symfony template inheritance your custom template should have the same path ant name than the original one so if you are trying to replace SonataAdminBundle:CRUD:base_list_field.html.twig your custom template should be in app/Resources/view/CRUD/base_list_field.html.twig
I suggest that you use full path to template:
app/Resources/views/sonata/imagepreview.html.twig
->add('imageFilename', null, [
'template' => 'sonata\imagepreview.html.twig',
'label' => 'Bild'
]);
So, as you can sonata\imagepreview.html.twig is relative to app/Resources/views/ folder.
In symfony of version < 4.x we have two ways of placing the twig tempates:
Inside the bundle
RealPath: `src\AppBundle\Resources\views\MyCustomFolder\my_file.html.twig`
Path: `AppBundle::MyCustomFolder\my_file.html.twig`
Outside the bundle in app folder
RealPath: `app\Resources\views\MyCustomFolder\my_file.html.twig`
Path: `MyCustomFolder\my_file.html.twig`
You can read more in Official Symfony documantation
I have a user table, and a list in Sonata Admin.
Also, I have a file table, where the users are connected to the files with a user_id field.
Now, this is the config for the list of the users. So far so good, it works.
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('username')
->addIdentifier('email')
->addIdentifier('firstName')
->addIdentifier('lastName')
;
}
Tried to find it in the documentation, but it's unclear to me how do I add a field to the list, where the number of connected files are indicated, or even better, if there is at least one uploaded file for the user, I have a flag about it in the list in a separate field.
Thanks for your help in advance!
You have to create a custom template where you display the information you want :
list_files.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
<div>
{{ object.files|length }}
</div>
{% endblock %}
call it in your list method
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('username')
->addIdentifier('email')
->addIdentifier('firstName')
->addIdentifier('lastName')
->add('picture', null, array(
'template' => 'ApplicationSonataAdminBundle:User:list_files.html.twig'
));
;
}
You might need to adapt the template path.
Read this for more details : Sonata admin bundle preview image from some entity in list mapper without sonata media bundle
I have a number of forms on the one page under different tabs
After the form is processed, I would like to return to the same tab as the form was sent from.
Basically, I would like to modify the target_route to go to the current page with an Anchor at the end of the URL. (EG company/view/6#editdetails)
Could someone provide or link to an example that I can put in my controller or into twig?
The answer is simply:
$form = $this->createForm(new ContactType($contact), $contact, array(
'method' => 'POST',
'action' => '#editdetails'
));
You may also set this in the template itself as an attribute. See example:
{{ form_start(form, {attr: { novalidate: "novalidate", class: 'requiredStar', action: '#form' }}) }}