Hi,
I'm trying to display a choice-type field in a list. Everywhere, I use 'choice' type, pass the choice array, and the integer is rendered as the corresponding value (in my case, a human readable string). However, that doesn't happen in list view. No matter what field type I assign to it, I always get an integer displayed (the array key), not the corresponding value.
Can anything be done regarding this?
cheers
Can you please paste here your code?
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title', null, array('name' => 'Title'))
->add('language', 'trans', array('name' => 'Language', 'catalogue' => 'SimbioticaMyBundle'))
->add('published', 'choice', array('choices' => SimbioticaMyBundle::getPublishedStatus()))
->add('url', null, array('name' => 'URL'))
->add('provider_name', 'trans', array('catalogue' => 'SimbioticaMyBundle'))
;
}
And the relevant array:
public static function getPublishedStatus()
{
return array(
self::STATUS_PUBLIC => 'Public',
self::STATUS_REVISION => 'Under revision',
self::STATUS_UNTRANSLATED => 'Untranslated',
self::STATUS_UNPUBLISHED => 'Unpublished',
self::STATUS_DELETED => 'Deleted',
);
}
The constants are ints, which are also stored in db, and are being printed instead of used as array indexes to fetch the matching strings
choice will not work in configureListFields. It's used to define form element in configureFormFields.
If you want to render custom <select>..</select> use custom template instead. This template is completely in your hands, so you can add there some javascript (and ajax) to handle changes and to store it to database.
Anyway, it will be nice, if SonataAdmin would support this feature by default. As far as I know only boolean list type is directly editable from list (with option editable=true)
I think you missed my point. I don't want a select or ajax action or anything. I just want the matching string to be displayed, instead of the integer that's stored in the db entry and that I use everywhere else successfully as the array index to get the matching human-friendly string.
Exemplifying: currently, with the above example I'm getting the following:
someTitle | english | 3 | some.url.com | someProviderName
and I want to get:
someTitle | english | Untranslated | some.url.com | someProviderName
Just that. Read-only. No JS magic ;)
My goal with this bug report is that someone with more knowledge of SonataAdmin than me can do that implementation, as IMO it's not such a farfetched thing (I'm surprised it didn't show up earlier) and I guess it shouldn't be that hard to implement if you know where to do the changes.
Use custom template.
Change definition of field:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('published', 'string', array('template' => 'AcmeDemoBundle:CRUD:list_publish_status.html.twig'));
//...
}
Create file list_publish_status.html.twig in AcmeDemoBundle/Resources/views/CRUD.
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% spaceless %}
{% if value is empty %}
{% else %}
{{ ('publish_status_'~value)|trans({}, "admin") }}
{% endif %}
{% endspaceless %}
{% endblock %}
Create translation file admin.en.php (if you don't have any) in AcmeDemoBundle/Resources/translations with this content:
<?php
return array(
'publish_status_1' => 'Public',
'publish_status_2' => 'Under revision',
//etc.
);
Ok, will try it, and see if I can apply those directly to the bundle, to provide native support, and PR it
thanks
The better way of solving your problem might be to create new database table published_status which will have reference to your main table. Sonata will then use the content of published_status table automatically base on __toString() method.
IMHO that's not the best solution. Having to query the db here and everywhere for a value that's hardly gonna change (it's a publishing status list, how often do those change in the real world?) it's not a practical solution from several points of view. I'd really appreciate if someone could do this change in the actual bundle. Like I said, I'm not an expert on the internal workings of SonataAdmin. I'll try to do it myself, but no guarantees. And given the relatively small changes needed to implement this as a custom template, I'm guessing it shouldn't be that hard to implement at core level, if one knows where to apply those changes.
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('getPublishStatusName', 'string');
//...
}
The getPublishStatusName must return the status name.
i don't understand, i try this
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id', null)
->addIdentifier('thread', null, array('label' => 'Identifiant / Section'))
->add('body', null, array('label' => 'Méssage'))
->add('getPublishStatusName', 'string', array('label' => 'Status'))
;
}
public function getPublishStatusName()
{
return array('0' => 'Validé', '1' => 'Supprimé', '2' => 'Spam', '3' => 'En attente validation');
}
but it return nothing...
i want a choice list in the list fields.
it works for datagrid and formmaper, and don't for listmapper
I think you have to correct to something like this:
public function getPublishStatusName()
{
$statuses = self::getPublishedStatus();
return $statuses[$this->getId()}
}
doesn't work
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id', null)
->add('thread', null, array('label' => 'Identifiant / Section'))
->addIdentifier('body', null, array('label' => 'Méssage'))
->add('state', 'doctrine_orm_choice', array('label' => 'Status', 'choices' => array('0' => 'Validé', '1' => 'Refusé', '2' => 'Spam', '3' => 'En attente validation')))
->add('published', 'doctrine_orm_choice', array('choices' => self::getPublishedStatus()))
;
}
public static function getPublishedStatus()
{
return array(
'0' => 'Validé', '1' => 'Supprimé', '2' => 'Spam', '3' => 'En attente validation'
);
}
someone can help me to do this ?
Please review this exemple: https://github.com/sonata-project/SonataNotificationBundle/blob/master/Admin/MessageAdmin.php#L53-L76
and don't try to use form element inside a list.
call to undefined method getStateList
if i use mine getPublishedStatus, this is same, it show 0, 1 or 2 but not validé, supprimé
i don't get it...
Thank you @rande your getStatusName() trick was really helping !
Please add support for 'multiple' option
@sirian please, give us more informations - your use case, some example etc. to a new issue.
@pulzarraider
protected function configureListFields(ListMapper $list)
{
$list
->addIdentifier('id')
->add('roles', 'choice', [
'multiple' => true,
'choices' => ['ROLE_USER' => 'user', 'ROLE_ADMIN' => 'admin']
])
;
}
Should be displayed on list page as
| Id | Roles |
| --- | --- |
| 1 | user |
| 2 | user, admin |
now throws Exception "Array to string conversion"
BTW @pulzarraider , you mentioned:
Anyway, it will be nice, if SonataAdmin would support this feature by default. As far as I know only boolean list type is directly editable from list (with option editable=true)
Has the editable feature for rendering select / choice e.g. using one-to-one relations been enabled yet?
Possibly related issues:
@webdevilopers As far as I know any relations with editable feature are not yet supported.
@sirian your issue was fixed in PR #1739 (This note is added here for all, who will search for this feature).
Is editable feature added yet for select or choice? as @webdevilopers has asked 2 years ago? Just wondering if something changed, because need to implement it myself.
is it implemented?
It's the real question, yes. How to implement choice in list fields with editable => true.
With custom template there should be JS which will make api (also customly written) request and update field in html accordingly, yes?
I'm also interested in this feature.
How I see in version 3.66, this feature not work. I have id in entity and function to 'translate' it to text values return id!
Thank you @rande your
getStatusName()trick was really helping !
Can you show function code?