Here is the code used to create the form:
$form = $this->createFormBuilder($city)
->add('name', 'text')
->add('images', 'collection', array('type' => new ImageType())
->getForm();
City images property, is a collection of Image entity.
Here the definition of ImageType
class ImageType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('path', new ImageFieldType());
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array('data_class' => 'My\Bundle\Entity\Image',));
}
public function getName() {
return 'image';
}
}
Here the code for my custom field type ImageFieldType:
class ImageFieldType extends AbstractType {
public function getDefaultOptions(array $options) {
return array('path' => null);
}
public function getParent() {
return 'field';
}
public function getName() {
return 'imageField';
}
}
In order to render the ImageFieldType I use the following block:
{% block imageField_widget %}
{% if form.vars.value is not null %}
<a href="{{ path('_cityManage') }}/CityID/ImageID">
<img class="image_type" src="{{ asset('uploads/documents/' ~ form.vars.value) }}" />
</a>
{% endif %}
{% endblock %}
How can I get the city.id and current image.id in this twig block? I’ve tried with {{ id }}, but this print something like form_image_0_path.
My goal is to have a link to the page path('_cityManage')/city.id/image.id
What I do in cases like these (I’m not sure if this is the best way to do it, but it works):
In your case, in the controller I’d pass the variables you’d need when calling the render function:
You can now use these variables in your template:
Again, I’m not sure if this is actually the best way to do it, but it works for me at the moment and I’m not aware of any other methods…