i try to use the search block in Sonata admin:
An exception has been thrown during the rendering of a template ("Class does not exist").
and these errors:
}, { 'query': query, 'admin_code': admin.code, 'page': 0, 'per_page': 10, 'icon': group.icon }) }} {% endif %} {% endfor %} {% endif %} {% endfor %}
this problem is displayed when no entity exists for some Bundle, i mean Bundles without Entities.
I solved this Problem by escaping these Bundles in search.html.twig:
{% if admin.code!= 'bundle.admin.bulkcoupon' and admin.code!='bundle.admin.tools' %}
{{ sonata_block_render({
'type': 'sonata.admin.block.search_result'
}, {
'query': query,
'admin_code': admin.code,
'page': 0,
'per_page': 10,
'icon': group.icon
}) }}
{% endif %}
Related
I've an User entity and EasyAdmin (EA) UserCrudController for manage to them.
The User entity has active boolean field. And I want that field will be disabled for current user in admin interface.
I got a working solution:
{% extends '#EasyAdmin/crud/index.html.twig' %}
{% block table_body %}
...
{% for field in entity.fields %}
{# disable active field for current uset #}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{% if app.user.id == entity.instance.id and field.property == 'active' %}
{% set templatePath = 'admin/crud/field/_boolean_disabled.html.twig' %}
{% else %}
{% set templatePath = field.templatePath %}
{% endif %}
{% endif %}
<td data-label="{{ field.label|e('html_attr') }}" class="{{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
{{ include(templatePath, { field: field, entity: entity }, with_context = false) }}
</td>
{% endfor %}
...
with override EA boolean template.
But I wouldn't want to override EA boolean template and only complete field.formTypeOptions by element 'disabled': 'true'
{% for field in entity.fields %}
{# disable active field for current uset #}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
{% if app.user.id == entity.instance.id and field.property == 'active' %}
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
{% endif %}
{% endif %}
<td data-label="{{ field.label|e('html_attr') }}" class="{{ field.property == sort_field_name ? 'sorted' }} text-{{ field.textAlign }} {{ field.cssClass }}" dir="{{ ea.i18n.textDirection }}">
{{ include(field.templatePath, { field: field, entity: entity }, with_context = false) }}
</td>
{% endfor %}
But for this path I get an error: "Uncaught PHP Exception Twig\Error\SyntaxError: "Unexpected token "punctuation" of value "." ("end of statement block" expected)." at /home/vagrant/code/blog.local/templates/admin/crud/user/index.html.twig line 27"
line 27: {% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
When I do:
{% set x = field.formTypeOptions|merge({'disabled': 'true'}) %}
{{ dump(x) }}
array:7 [▼
"required" => false
"row_attr" => array:1 [▶]
"attr" => array:1 [▶]
"label" => "Active"
"label_translation_parameters" => []
"label_attr" => array:1 [▶]
"disabled" => "true"
]
I get the required array, but I get the same error when I try to assign a new value
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
What am I doing wrong? Thanks
I think its because the merge function does not like punctuations. Try to set the value to a variable before:
Change:
{% set field.formTypeOptions = field.formTypeOptions|merge({'disabled': 'true'}) %}
To:
{% set options = field.formTypeOptions %}
{% set field.formTypeOptions = options|merge({'disabled': 'true'}) %}
You could already do that in UserCrudController and avoid that logic in Twig:
public function configureFields(string $pageName): iterable
{
// ...
// check user/roles
$isInputDisabled = true;
if($this->isGranted('ROLE_ADMIN')){
$isInputDisabled = false;
}
// ...
$active = BooleanField::new('active', 'Active')
->setFormTypeOption('disabled', $isInputDisabled);
// ...
}
Within my Symfony 3.4 project, I have 2 custom admins. Specially created for reporting services. Those admins do not have specific entities.
For the custom admins, I followed the Symfony recipe:
https://symfony.com/doc/3.x/bundles/SonataAdminBundle/cookbook/recipe_custom_view.html
Now, when searching items through the sonata global search, I get a
"Class does not exist" error in
vendor/sonata-project/admin-bundle/src/Resources/views/Core/search.html.twig.
This error is related to the custom admins.
Is there a solution to exclude these custom admins from the global search or to resolve this error?
Admin:
<?php
namespace MainBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollection;
class AccessRightsAdmin extends AbstractAdmin
{
protected $baseRoutePattern = 'accessrights';
protected $baseRouteName = 'Accessrights';
protected function configureRoutes(RouteCollection $collection)
{
$collection->clearExcept(array('list'));
$collection->add('accesRights', 'accessrights');
}
}
Service
services:
system.admin.accessrights:
class: MainBundle\Admin\AccessRightsAdmin
arguments: [~, ~, MainBundle:AccessRightsAdmin]
tags:
- { name: sonata.admin, manager_type: orm, group: sonata.admin.group.System, label: Accessrights }
calls:
- [ setTranslationDomain, [SonataAdminBundle]]
public: true
I found a solution and I'm going to leave it here in case someone need it.
The solution basically is to override the search.html.twig and ignore the admin you want from the search like so:
{% extends base_template %}
{% block title %}{{ 'title_search_results'|trans({'%query%': query}, 'SonataAdminBundle') }}{% endblock %}
{% block breadcrumb %}{% endblock %}
{% block content %}
<h2 class="page-header">{{ 'title_search_results'|trans({'%query%': query}, 'SonataAdminBundle') }}</h2>
{% if query is defined and query is not same as(false) %}
{% set count = 0 %}
<div class="row" data-masonry='{ "itemSelector": ".search-box-item" }'>
{% for group in groups %}
{% set display = group.roles is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}
{% if display %}
{% for admin in group.items %}
{% set count = count + 1 %}
{% if admin.code != 'bundle.admin.admin_to_ignore' %}{# in this line right here add the admin you want to ignore in your search #}
{% if admin.hasRoute('create') and admin.hasAccess('create') or admin.hasRoute('list') and admin.hasAccess('list') %}
{{ sonata_block_render({
'type': 'sonata.admin.block.search_result'
}, {
'query': query,
'admin_code': admin.code,
'page': 0,
'per_page': 10,
'icon': group.icon
}) }}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endblock %}
To override the file you need to put it under the following path:
app -> Resources -> SonataAdminBundle -> views -> Core -> search.html.twig
A little question : how can I change < title > < /title > with Sonata Admin ?
Everywhere there is "Admin...". And when I override a page with sonata template I got "Admin".
Thank's.
You can easy do that by override the sonata admin file translation
1- Create folder structure in traslations like this :
2- then in the SonataAdminBundle.[your-language-code].yml file add :
Admin: "You Custom title Here"
You have to override Sonata admin's standard_layout.html.twig file to achieve this.
First, define the file in sonata admin config file.
config/packages/sonata_admin.yaml
sonata_admin:
templates:
layout: 'sonata_admin/layout.html.twig'
Create a twig file named layout.html.twig inside templates/sonata_admin/ and just paste the sonata_head_title block from the bundle.
{% extends '#SonataAdmin/standard_layout.html.twig' %}
{% block sonata_head_title %}
{{ 'Admin'|trans({}, 'SonataAdminBundle') }} //remove this line to get rid of text "Admin"
{% if _title is not empty %}
{{ _title|striptags|raw }}
{% else %}
{% if action is defined %}
-
{% for menu in breadcrumbs_builder.breadcrumbs(admin, action) %}
{% if not loop.first %}
{% if loop.index != 2 %}
>
{% endif %}
{%- set translation_domain = menu.extra('translation_domain', 'messages') -%}
{%- set label = menu.label -%}
{%- if translation_domain is not same as(false) -%}
{%- set label = label|trans(menu.extra('translation_params', {}), translation_domain) -%}
{%- endif -%}
{{ label }}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endblock %}
I am currently creating a module on Grav but I have a problem.
Yaml :
table:
-
title: Test
content:
-
value: 'test1'
-
value: 'test2'
-
value: 'test3'
Twig :
{% for item in page.header.table.content %}
<h1>{{item.value}}</h1>
{% endfor %}
I can not display 'value'
but if I do :
{% for item in page.header.table %}
<h1>{{item.title}}</h1>
{% endfor %}
title is displayed correctly
Because Table is an array of object
https://twigfiddle.com/jbde9m
{% for item in page.header.table %}
<h1>{{item.title}}</h1>
{% endfor %}
{# you can also do for item in page.header.table.0.content #}
{% for item in (page.header.table|first).content %}
<h1>{{item.value}}</h1>
{% endfor %}
i have a problem with twig syntax and merge function ... I have multiple object with 2 field category and price.
I need to create an array or hash (i guess hash is easier but ... i try both) with sum of prices for each category.
So i try many code, and my last is :
{% set test = [ {'category': 'description', 'price': '1'}, { 'category': 'abc', 'price': '2'}, { 'category':'description', 'price': '3'} ] %}
{% set listCategory={} %}
{% for line in test %}
{% set new_category = { 'category': line.category, 'price': line.price } %}
{% if loop.first %}
{% set listCategory = listCategory|merge([new_category]) %}
{% else %}
{% set flag = false %}
{% for category in listCategory %}
{% if line['category'] == new_category['category'] %}
{% set tmp = line['price'] + new_category['price'] %}
{# i try it too#}
{% set category = category|merge([tmp]) %}
{# or i try this#}
{% set category = category|merge({ (category.price) : category.price + new_category.price }) %}
{{ dump(listCategory) }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
I try it since 3 hours and i don't know where i make an error.
When i check my array, i test if the key 'name' exist
if yes, i want to add the price of element to the hash price
if no, i want to add a new array in hash with key = 'name'
Anyone have an idea ? thx for your reading.
I think you are looking for something similar to:
{% set test = [ {'category': 'description', 'price': 1}, { 'category': 'abc', 'price': 2}, { 'category':'description', 'price': 3} ] %}
{% set listCategory={} %}
{% for line in test %}
{% set key = line.category %}
{% if listCategory[key] is defined %}
{# notice here that the key is in brackets () because otherwise it will be interpreted as the string "key" %}
{% set listCategory = listCategory|merge({(key):listCategory[line.category]+line.price}) %}
{% else %}
{% set listCategory = listCategory|merge({(key):line.price}) %}
{% endif %}
{{ key }}: {{ listCategory[key] }}
{% endfor %}