Closed mleko64 closed 4 years ago
Something like this problem happens to me when I want to replace a template from the calls section within the service. I can only call the function if I put as an @required annotation.
Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
This thread has been stale & closed, but is relatively high in Google results for this issue. I'd like to post a workaround for people (like me) affected by this – don't rely on auto-loaded block names.
Example - my form field before:
->add('someFieldName', ModelAutocompleteType::class, [
'class' => SomeObject::class,
'multiple' => true,
'by_reference' => false,
'required' => false,
'property' => 'name',
'placeholder' => '',
'btn_add' => false,
'to_string_callback' => '…',
])
Twig html block to override the label that was working fine before:
{% block admin_someAdminName_someFieldName_sonata_type_model_autocomplete_label %}
here I override the label
{% endblock %}
To work around the issue, give a unique block_prefix
to the form field. The form field after:
->add('someFieldName', ModelAutocompleteType::class, [
'class' => SomeObject::class,
'multiple' => true,
'by_reference' => false,
'required' => false,
'property' => 'name',
'placeholder' => '',
'btn_add' => false,
'to_string_callback' => '…',
'block_prefix' => 'some_admin_some_field_name',
])
Now you can reference this name directly in Twig. Twig after:
{% block some_admin_some_field_name_label %}
here I override the label
{% endblock %}
Done! Hope it helps.
There are 4 overrides available in total (the block prefix + underscore + override name):
block_prefix_row
block_prefix_label
block_prefix_widget
block_prefix_help
Environment
Sonata packages
Symfony packages
PHP version
Subject
Sometimes, I need override form block fields (I believe that not only me) generates by SonataAdmin (to handle some cases). When I'm using SonataAdmin with Symfony 4, all block prefixes looks like this (example):
I can't override for example
App\Admin\User\User_firstName_text
block because Twig not allow me to create in Twig template a block with name which contains backslash characters.I know, that this name (first part of block name) is coming from Admin code (in Symfony 4 all Admin classes are registered in new way):
I found where names for form block prefixes are prepared:
sonata-project/admin-bundle/src/Form/Extension/Field/Type/FormTypeFieldExtension.php
In line 90 and 123 is prepared
$basename
variable from Admin code (first part of block prefix name):Only dot characters are replaced to underscores. In my opinion, backslash characters should be replaced too.
Steps to reproduce
Create new Admin class and register is as service in new way:
Expected results
All names for form block prefixes should by valid to override in Twig template (replace backslash characters to underscore characters):
Actual results
Cannot override form blocks, because block names contains backslash characters. These names are not valid for Twig.