Symfony 2, Gedmo Tree: dropdown select sorted by title - symfony

I'm trying to create a dropdown select menu from my directories. Each directory has subdirectories and I'm using a Gedmo Tree Extension to implement it. I created dropdown in this way:
$form = $this->createFormBuilder()
->add('parentDirectory', 'entity', array(
'required' => false,
'label' => 'Directories',
'class' => 'TestTestBundle:Directory',
'attr' => array('class' => 'hidden nextSelect 2-select'),
'empty_value' => 'Choose directory',
'property' => 'indentedName',
'multiple' => false,
'expanded' => false,
'query_builder' => function(DirectoryRepository $r) {
return $r->createQueryBuilder('c')
->orderBy('c.root', 'ASC')
->addOrderBy('c.lft', 'ASC');
}
It works fine, but I need to order all directories also by its title. Is there any way to do this?

This should work:
->orderBy('c.root, c.lvl, c.title', 'ASC')
'lft' doesn't take into account for 'lvl' and 'title'

Related

Access translated property of collection in form

I have a form with a collection. My form "PageType" with a collection look like this.
$builder->add('sections', 'collection', array(
'type' => new SectionType($this->securityContext, $this->entityManager) ,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
'prototype' => true,
'prototype_name' => '__sectionPrototype__',
'options' => array(
'label' => false,
)
And my second form which represents the collection "SectionType" look like this.
->add('translations', 'a2lix_translations', array(
'fields' => array(
'title' => array(
'field_type' => 'text',
'label' => 'title',
'attr' => array('class' => 'rte sectionTitle')
),
'text' => array(
'field_type' => 'textarea',
'label' => 'hintText',
'attr' => array('class' => 'rte sectionDescription')
)
)
))
How can i access from twig the translations fields?
I think that is not possible. Maybe somebody could correct me if I'm wrong.
fields is an option item for a2lix_translations. If you would have Form object you might be able to do that, but in fact, you have FormView which represents built form.
Alternatively, you could pass that array via standard return from controller and into your twig. Does that sound good?

Symfony2 collection and related choice field not (yet) managed

I've got a form with a collection field "options" and a choice field "defaultOption". My goal is to enable the user to create a new entity, add some options and select a default option from the list of option he/she just added before saving the entire thing.
Here's part of the form that shows both fields:
$form->add('options', 'collection', array(
'type' => new CustomFieldOptionType(),
'label' => 'custom.field.options',
'allow_add' => true,
'allow_delete' => true,
'options' => array(
'label_render' => false,
'widget_control_group' => false,
),
'by_reference' => false,
'attr' => array('class' => 'options')
));
$form->add('defaultOption', 'entity', array(
'label' => 'custom.field.default',
'class' => 'XFDOBundle:CustomFieldOption',
'choices' => $field->getOptions(),
'property' => 'name',
'required' => false,
'empty_value' => 'custom.field.default.empty',
'attr' => array('class' => 'default-option')
));
I've tried several things with jquery and form event listeners to (re)populate the "defaultOption" field with the new (not-yet-persisted) options, but that resulted in to nothing or "Entities passed to the choice field must be managed" exceptions.
Any idea how I can get this working?

Set default value on entity list in Symfony

I defined a form entity list:
->add('businessTime', 'entity', array(
'class' => 'AcmeDemoBundle:BusinessTimes',
'property' => 'name',
'required' => true
);
With this the list is displayed but with blank option as default. I would like to display a selected option from the entity by default.
you can use the data property for that
->add('businessTime', 'entity', array(
'class' => 'AcmeDemoBundle:BusinessTimes',
'property' => 'name',
'data' => 1234456,
'required' => true
);

The form Entity type

Anyone has any idea how to add some custom options to an entity field type in a nice and clean way? e.g
->add('groups', 'entity', array(
'class' => 'MyBundle:Groups',
'multiple' => 'true',
'query_builder' => function(GroupsRepository $er) {
return $er->createQueryBuilder('g')
->orderBy('g.name', 'ASC');
}))
Let's say that query builder returns "Group1", "Group2" and "Group3". What I want is to "inject" another option called "Any Group" for example.
Thanks,Stelian
The EntityType is a child of the ChoiceType. So, you have access to the same options.
->add('groups', 'entity', array(
'class' => 'MyBundle:Groups',
'multiple' => 'true',
'query_builder' => function(GroupsRepository $er) {
return $er->createQueryBuilder('g')->orderBy('g.name', 'ASC');
},
'empty_value' => 'Any group',
))

using sonata_type_collection against a custom form type instead of a property/relationship with another entity

is it possible to use sonata_type_collection against a custom form type instead of a property/relationship with another entity?
Something like this:
$formMapper->add('foo', 'sonata_type_collection',
array('type' => new \myVendor\myBundleBundle\Form\Type\BlockType()
));
Which throws me the following error
The current field `contingut` is not linked to an admin. Please create one for the target entity : ``
EDIT:
Something like this did the trick:
$formMapper->add('contingut', 'collection', array(
'type' => new \myVendor\myBundleBundle\Form\Type\Block1Type(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
Instead of a custom type in your example, you can also use a native entity type.
$formMapper->add('cars',
'collection',
array(
'type' => 'entity',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'options' => array(
'class' => 'YourBundle:Car',
'property' => 'name',
'label' => false
)
)
)

Resources