datagrid filter for relation object as text field (insted of dropdown) in sonata admin in symfony 2.4 - symfony

I have entity 'Action' with relation to 'User'. Created Admin CRUD controller in SonataAdminBundle. Everything works fine except user filter is rendered as dropdown list. I have 8k user count and growing so you must see why this is a problem.
I want user filter to be text input and on submit to search with LIKE %username%
Right now I add user filter like this - $datagridMapper->add('user').
I know I can add filter type and field type but I am not able to find the right combination and options. Found information on http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html but still no success.
Final solution
Following Alex Togo answer I used this code:
$datagridMapper->add('user', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (empty($value['value'])) {
return;
}
$queryBuilder->leftJoin(sprintf('%s.user', $alias), 'u');
$queryBuilder->where('u.username LIKE :username');
$queryBuilder->setParameter('username', '%'.$value['value'].'%');
return true;
},
'field_type' => 'text'
))

I needed something like this on a project. I implemented this feature using this. You can try to set the 'field_type' option to 'text' (I used 'choice' in the project I worked at) and add the querybuilder actions you need to filter.

Use doctrine_orm_choice option.
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add(
'module',
'doctrine_orm_choice',
[],
'choice',
[
'choices' => $this->filterModuleList
]
)
....

Related

How can I set a value in Twig when the select is built using EntityType?

In a Form say I have a builder option like this:
->add('choice', ChoiceType::class, [
'choices' => [
'Cheese' => 'cheese',
'Plain' => 'plain
]
])
And let's say we are editing this option, in the database they've already selected plain. With twig we can write the widget like this:
{{ form_widget(BurgerForm.choice, {
'value': burger.type
}) }}
This will make the value in the database the pre-selected value for the select. But if you do the same thing with EntityType:
->add('choice', EntityType::class, [
'class' => 'AppBundle:BurgersTypes',
'choice_label' => 'type'
])
And you use the same twig it doesn't pre-select the option from the database. How can I get the value from the database to show as the pre-selected value of the widget?
Pre-selecting a value for this form means setting a value on the underlying data. In your case, the controller ought to look something like:
// src/AppBundle/Controller/MyController.php
namespace AppBundle\Controller\MyController;
use AppBundle\Entity\Order;
use AppBundle\Entity\BurgersTypes;
use AppBundle\Form\Type\FormType;
use Symfony\Component\HttpFoundation\Request;
public function formAction(Request $request)
{
$obj = new Order();
$defaultBurgerChoice = new BurgersTypes('cheese');
$ob->setChoice($defaultBurgerChoice);
$form = $this->create(FormType::class, $obj);
$form->handleRequest($request);
...
// Now, if the form needs to render a value for `choice`,
// it will have the value of BurgerForm.choice determined
// intentionally - by your default, or overridden and
// handled in the request!
return [
'BurgerForm' => $form->createView()
]
}

Drupal change multi select to checkboxes on views exposed form not working

I needed to have all taxonomy vocabularies available to filter on, without adding all of the taxonomies one at a time.
I did this using the Content: Has taxonomy terms (Multiple) Filter - which renders out as a multiple select list.
I needed to change the select list to checkboxes, but BEF didnt allow me to do that for this type of field, so i did the following...
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form') {
$options = $form['filter']['#options'];
unset($form['filter']);
foreach($options as $vocab => $terms) {
foreach ($terms as $key => $value) {
$newkey = $options[$vocab][$key]->option;
$termoptions[$vocab][key($newkey)] = $newkey[key($newkey)];
}
$form[$vocab] = array(
'#type' => 'checkboxes',
'#options' => $termoptions[$vocab],
'#title' => $vocab,
'#multiple' => TRUE,
);
}
}
}
The exposed form looks good, but it doesnt work.
I think its because the name of the query is wrong. As i have split up the heirarchy into separate fields, the url used to look like
mysite.com/category?filter[]=123
Now it looks like...
mysite.com/category?Brand[123]=123
So theres how far ive got, any ideas how i can make this exposed form work?
I had a poke around at changing the submit handler views_exposed_form_submit but i dont know what i would need to change.
i am updating my ans once again for more clear view
step one expose filter
settings for this
click on settings text then
then gor for bef
and the result is
hope it make sense now

How to go directly in the edit section in Backpack-Laravel?

I need that when you to click on an item in the Backpack sidebar (For ex. Profile),
I immediately can edit the information in this page (For ex. the user who is writing, then the name, surname, date of birth etc.) without going through the table, typical visual of backpack.
In a simple manner:
The user should have the ability to change their profile information whith one click.
How can I do this in Backpack-laravel?
Anyone have any ideas?
Starting code:
ProfileCrudController.php
public function __construct()
{
parent::__construct();
}
public function edit($id)
{
return parent::edit($id);
}
If you mean clicking on the user name in the sidebar should go to editing that user, you can do that by placing the link on it in your /resources/views/vendor/backpack/base/inc/sidebar.blade.php.
I do not know if it might be the correct way. But it's the only way I've found to accomplish what I had in mind.
public function index()
{
$this->crud->addFields([
[
'name' => 'name',
'label' => 'Name',
'type' => 'Text'
],
[
'name' => 'surname',
'label' => 'Surname',
'type' => 'Text'
]
]);
return parent::edit(Auth::id());
}

Sonata how make autocomplete filter on current entity property

I'm working on a sf2 project using Sonata Admin Bundle.
The project is a Donations website for humanitarian mission.
I have a 'Personne' entity, represent benefactors (donation makers).
My problem is the following:
I have to filter results in the sonata list view using autocompletion.
I want filter results using the 'name' property of the current entity ('Personne').
What I'm expecting :
$datagridMapper
->add('personne', 'doctrine_orm_model_autocomplete',
array('label' => 'AutoComplete'),
null,
array('property' => 'name'))
// error output : " The option `association_mapping` must be set for field: `personne` "
You can see my full admin class and entity on this gist :
https://gist.github.com/chalasr/0658a02b1c04180f5563
I understand this field type is reserved to entity associations (by example I already use it for filter results of my Donation entity by Personne name (other admin class).
My question is :
Is it possible to do what I need ?
If I can't do that using this field type, what is the right way to achieve this task ?
Thank's for your help.
After a lot of tests, it seems this functionality isn't yet provided by Sonata.
So, I had build an homemade autocomplete method in my admin controller and used it as ajax in my overridden CRUD:list.html.twig template.
This method takes field name, other autocomplete fields values, and keyword as parameters and reload results on keyup event.
You can look on this gist:
https://gist.github.com/chalasr/5c27ae64dc596967f18a
If you have an idea/proposition for optimize my code (simple autocomplete field type for $formMapper ?), I'm really interested.
Well it's possible to make a choice field and use integrated select2 to handle autocomplete, I don't know how well it would fare with large tables though.
$datagridMapper
->add('personne','doctrine_orm_callback', array(
'callback' => array($this, 'filterByName'),
'field_type' => 'text',
), 'entity',array(
'class' => 'AppBundle\Entity\Personne',
'choice_label' => 'name'
))
We add 'doctrine_orm_callback' because regular string filter can't handle EntityType Field, so we need to do it ourselves.
public function filterByName($queryBuilder, $alias, $field, $value)
{
if (!$value['value']) {
return;
}
$queryBuilder
->andWhere($alias . '.name' . ' = ' . ':name' )
->setParameter('name' , $value['value']->getName());
return true;
}
1 more thing, select2 won't create autocomplete (search) box if dropdown box has less than 10 choices, because it's set up in Admin.js that way.
select.select2({
width: function(){
// Select2 v3 and v4 BC. If window.Select2 is defined, then the v3 is installed.
// NEXT_MAJOR: Remove Select2 v3 support.
return Admin.get_select2_width(window.Select2 ? this.element : jQuery(this));
},
dropdownAutoWidth: true,
minimumResultsForSearch: 10,
allowClear: allowClearEnabled
});
so you need to override it, if you want that to be less.
Fonctionnality is now bundled with Sonata as simple as:
$filter->add('label', ModelFilter::class, ['field_type'=>ModelAutocompleteType::class,'field_options' => ['property' => 'name']])

Sonata admin, editable field with choice

I'm using sonata admin and there is an option 'editable' => true for edit directly inline datas on the list view.
If my field is a text, it's ok, i can click, edit the text and save directly on the table.
But i don't want an input type="text" when i click on the field, but a list, i'm trying something like :
->add('etat', null, array('editable' => true), 'choice', array(
'choices' => array(
'Brut' => 'Brut',
'NRP' => 'NRP',
)
))
But no effetc.. is this possible ?
Since Sonata Admin Bundle 2.2 choice accepts the "editable" parameter in the list view. You use it like that:
$listMapper->add('etat', 'choice', [
'choices'=>['Brut'=>'Brut', 'NRP' => 'NRP',],
'editable'=>true,
]);
Doc: https://sonata-project.org/bundles/admin/2-2/doc/reference/field_types.html
It's possible for scalar values only. Hear some doc
http://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
Well it's not possible at the moment and won't be possible as I can guess in the near future. Your own realization should be written.

Resources