I want to edit the default form template to include a thumbnail preview of an image when I display an upload field, so I put a method called getFormThumbnail() in my entity that returns the path. When the entity has the method it works, but when it doens't I get an error:
An exception has been thrown during the rendering of a template ("Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Acme\UserBundle\Entity\UserTranslation' does not have a method 'getFormThumbnail' in C:\...\symfony\vendor\knplabs\doctrine-behaviors\src\Knp\DoctrineBehaviors\Model\Translatable\TranslatableMethods.php line 140") in kernel.root_dir/Resources/views/Form/form_div_layout.html.twig at line 43.
500 Internal Server Error - Twig_Error_Runtime
I am editing the code in {% block form_widget_simple %}
I added :
{% if type == 'file' %}
{% if form.vars.form.parent.vars.value.getFormThumbnail is not null %}
<img src="{{ form.vars.form.parent.vars.value.getFormThumbnail | imagine_filter('thumb_250') }}" style="display: block; margin-bottom: 10px">
{% endif %}
{% endif %}
This code works perfectly when getFormThumbnail exists. So I tried adding is defined and it doesn't work, I even tried things like:
form is defined
and
form is not null
and
form.vars is defined
and
form.vars is not null
and
form.vars.form is defined
and
form.vars.form is not null
and
form.vars.form.parent is defined
and
form.vars.form.parent is not null
and
form.vars.form.parent.vars is defined
and
form.vars.form.parent.vars is not null
and
form.vars.form.parent.vars.value is defined
and
form.vars.form.parent.vars.value is not null
and
form.vars.form.parent.vars.value.getFormThumbnail is defined
and
form.vars.form.parent.vars.value.getFormThumbnail is not null
Still, I always get the same error, the line in the error by the way is the line where I test if getFormThumbnail is defined
So if the error is does not have a method is there a way to test if it has a method?
You can create a custom fragment only for that field. So you don't have to check if the method exists or not http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field.
Or you can create a form type extension: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html
Related
I've got this code:
Twig template:
<div class="content-items-wrapper">
{% for item in home.latest_posts %}
{% include "partials/content-item.twig" with item %}
{% endfor %}
</div>
In my theme file:
$context['home']['latest_posts'] = new Timber::get_posts([
'posts_per_page' => 6,
]);
When using these line of code I get the following notice:
Warning: array_merge(): Argument #2 is not an array in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86
Followed by a fatal error:
Catchable fatal error: Argument 1 passed to Twig_Template::display() must be of the type array, null given, called in /content/plugins/timber-library/vendor/twig/twig/lib/Twig/Environment.php(462) : eval()'d code on line 86 and defined in //content/plugins/timber-library/vendor/twig/twig/lib/Twig/Template.php on line 401
The strange thing is: when I don't use the include in the Twig template, but just use the contents of this include directly the errors are gone.
Also just putting one character (or even nothing) in the included partial gives the errors.
Also when not using Timber::get_posts(), but just filling an array with the correct data, I don't get any errors.
But obviously both are not really wanted solutions :-)
What could this be?
PS: I've got a frontend-only version (running of gulp-twig), feeded by JSON data, which is just running fine.
Edit: fixed the example (copy/paste mistake)
Don't you need to add the context home.items like $context['home'] or $context['home']['latest_posts'] just like with $context['posts']?
Problem solved!
Changing
{% include "partials/content-item.twig" with item %} into {% include "partials/content-item.twig" with { 'item' : item } %} did the trick.
When I use the path function in Twig, there is a strange problem with the '?' when I want to set GET parameter.
Example:
href="{{ path(brData.router, {(brData.slug): brData.url}) }}"
If I set now as brData.url: 'search?q=bla',
then twig is coding the url in this way:
domain.com/search%3Fq=bla , and the search can not be executed. How can I prepare the url in the righ form: domain.com/search?q=bla
Thanks
Nik
The use of domain.com/search?q=bla involves you have a route defined with the path /search.
If you have the route, just pass a parameter and its value without wrapping it in a ?key=[value].
Example assuming search_route is the name of your /search route:
{% set fieldValue = 'bla' %}
{% set url = path('search_route', {'q': fieldValue}) %}
So href="{{ url }}" will give href="domain.com/search?q=bla".
This question already has answers here:
Twig - dynamically replace GET parameter's value
(2 answers)
Closed 6 years ago.
In a Symfony 2.7 app, with Twig, I want to create generate pagination buttons.
I have a search page, with optional GET parameters :
Empty :
www.[...].com/search
Search by name :
www.[...].com/search ?name=john
Search by name and sort by relevance :
www.[...].com/search ?name=john&sort=relevance
Search by name and sort by relevance, page 2 :
www.[...].com/search ?name=john&sort=relevance&p=2
More than 10 possibles parameters, so I don't want to define a route pattern with all optional parameters.
Then, when I display the search results, I want to link next pages :
www.[...].com/search?name=john&sort=relevance &p=2
www.[...].com/search?name=john&sort=relevance &p=3
www.[...].com/search?name=john&sort=relevance &p=4
In the TWIG template I tried it so :
{% set param = app.request.get('_route_params') %}
{% if param is null %}
{% set param = {'p': page} %}
{% else %}
{% set param = param|merge({'p': page}) %}
{% endif %}
{{ page }}
But, because the optional parameters are not defined in the route, they are not in request.get('_route_params') and the link builded only define ?p=X.
Is there any other way to get queried GET parameters and to edit them creating a new url with an other p ?
In your twig template, you can use:
app.request.query.all
to get all your query parameters
I would like to know if it was possible to cast a String to an Int in twig. I try to see if a user has enough credit or not to buy a cours. For that, I calculate the amount of credit with a render in the template (because I need the value in the template, and I didn't found a better way to do it...) like this :
{% set creditUser %}
{{render(controller('L3L2EntraideBundle:Credits:sommeCredits'))}}
{% endset %}
But when I try to compare creditUser :
{% if creditUser < c.idCompetenceCours.prix %}disabled="false"{% endif %}
Symfony return me a beautiful error : An exception has been thrown during the rendering of a template ("Notice: Object of class Twig_Markup could not be converted to int") in L3L2UserBundle:Profile:modal_prendre_rdv.html.twig at line 21.
Any idea ? Thank you in advance for my first question on Stackoverflow and sorry for my english.
This is not string but Twig_Markup
{% if creditUser.__toString < c.idCompetenceCours.prix %}
but this is not good approach you should get this value from object/variable not from rendered template
If I run the controller "normally" the (Booking) Controller throws an exception I get my custom 500 error page.
If I render the controller inside another template, like so:
{% render "BookingBundle:Booking:list" %}
then I don't get anything just blank output.
Is there a way to get the error page to display here with out doing a try catch inside the controller?
When you use the render tag, you can specify some options as the third argument:
ignore_errors default FALSE in debug mode and TRUE otherwise
alt an alternative controller to execute in case of an error
The debug mode is activated by passing true as the value of the second kernel's constructor argument. You can see it in the front controller classes:
$kernel = new AppKernel('dev', true);
If you don't want to ignore errors, even in non-debug mode, you can do:
{% render 'BookingBundle:Booking:list' with {}, {'ignore_errors':false} %}