{"action":"created","issue":{"url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/4220","repository_url":"https://api.github.com/repos/sonata-project/SonataAdminBundle","labels_url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/4220/labels{/name}","comments_url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/4220/comments","events_url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/4220/events","html_url":"https://github.com/sonata-project/SonataAdminBundle/issues/4220","id":193293718,"number":4220,"title":"The list view custom data mapping is not supported or never explained + solution","user":{"login":"Arkemlar","id":19671202,"avatar_url":"https://avatars.githubusercontent.com/u/19671202?v=3","gravatar_id":"","url":"https://api.github.com/users/Arkemlar","html_url":"https://github.com/Arkemlar","followers_url":"https://api.github.com/users/Arkemlar/followers","following_url":"https://api.github.com/users/Arkemlar/following{/other_user}","gists_url":"https://api.github.com/users/Arkemlar/gists{/gist_id}","starred_url":"https://api.github.com/users/Arkemlar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Arkemlar/subscriptions","organizations_url":"https://api.github.com/users/Arkemlar/orgs","repos_url":"https://api.github.com/users/Arkemlar/repos","events_url":"https://api.github.com/users/Arkemlar/events{/privacy}","received_events_url":"https://api.github.com/users/Arkemlar/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-12-03T15:29:03Z","updated_at":"2016-12-03T18:13:19Z","closed_at":null,"body":"I wasted a tonn of time to find out a way to make custom mapping for list view. It is never explained or I just not found it using google.\r\nSetting castom DataMapper instance for edit view was easy:\r\n```\r\n    protected function configureFormFields(FormMapper $formMapper)\r\n    {\r\n        $formMapper\r\n            ->...\r\n            ->add('name_en',TextType::class, ['empty_data'  => null])\r\n        ;\r\n        $formMapper->getFormBuilder()->setDataMapper($this);\r\n    }\r\n```\r\nBut list view doesn't use DataMapper // symfony Forms to show list data - it is used only for filters.\r\nMy solution was to create my own FieldDescription class and override getFieldValue method:\r\n```\r\nuse Sonata\\DoctrineORMAdminBundle\\Admin\\FieldDescription;\r\n\r\nclass ClosureAccessFieldDescription extends FieldDescription\r\n{\r\n    public function getFieldValue($object, $fieldName)\r\n    {\r\n        if($accessor = $this->getOption('accessor'))\r\n        {\r\n            return $accessor($object);\r\n        }\r\n        // ...\r\n   }\r\n}\r\n```\r\nNow it is possible to use closure to get value form object. Since sonata instantiating FieldDescription classes dirctly in its code, I can't replace it with my ClosureAccessFieldDescription class without changing vendor code. But $listMapper->add() supports passing FieldDescriptionInterface object instead of string name (ListMapper:97), so I use it directly in configureListFields:\r\n```\r\n    protected function configureListFields(ListMapper $listMapper)\r\n    {\r\n        $fieldDescription = new ClosureAccessFieldDescription();\r\n        $fieldDescription->setName('name_ru');\r\n        $fieldDescription->setOptions(['accessor' =>\r\n            function($object){\r\n                return $object->getName('ru');\r\n            }\r\n        ]);\r\n        $listMapper->add($fieldDescription);\r\n```\r\n\r\nI can clone-modify-send request to add new 'accessor' option, but first of all I want to ask you: would you confirm my changes? If for some reason the answer is negative, then I wont waste more time on this problem.\r\nAlso I am wondering why there are no info about such issue? I trying to make my entities in DDD style so getters/setters is not working for me and Sonata fails. I think such issue could be covered or at least asked already but I not found anything.\r\n\r\nI know: I should use DTOs instead of Entities but there are no information (again!) about how to use Sonata with DTO. CRUDController analysis gives no help and I dont see easy way to rewrite controller and use DTO since database accessed deep inside Sonata bundle classes. To change it I need to dig deeper into vendor classes while some of them may have 1k+ lines of code (CRUDController - 1368 lines, AbstractAdmin class - 3325, wow!) - no thanks! The most easy way I see is to make child classes of my entities with ugly set of getters/setters... or make mapped superclass and inherit one class for Domain entities and one clas for Sonata DTO... but thats all sounds bad for me, its like making crutch instead of healing leg.\r\n\r\nKeywords for Google: Sonata, DataMapper, custom mapping, list veiw."},"comment":{"url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/comments/264655593","html_url":"https://github.com/sonata-project/SonataAdminBundle/issues/4220#issuecomment-264655593","issue_url":"https://api.github.com/repos/sonata-project/SonataAdminBundle/issues/4220","id":264655593,"user":{"login":"Arkemlar","id":19671202,"avatar_url":"https://avatars.githubusercontent.com/u/19671202?v=3","gravatar_id":"","url":"https://api.github.com/users/Arkemlar","html_url":"https://github.com/Arkemlar","followers_url":"https://api.github.com/users/Arkemlar/followers","following_url":"https://api.github.com/users/Arkemlar/following{/other_user}","gists_url":"https://api.github.com/users/Arkemlar/gists{/gist_id}","starred_url":"https://api.github.com/users/Arkemlar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Arkemlar/subscriptions","organizations_url":"https://api.github.com/users/Arkemlar/orgs","repos_url":"https://api.github.com/users/Arkemlar/repos","events_url":"https://api.github.com/users/Arkemlar/events{/privacy}","received_events_url":"https://api.github.com/users/Arkemlar/received_events","type":"User","site_admin":false},"created_at":"2016-12-03T18:13:19Z","updated_at":"2016-12-03T18:13:19Z","body":"Btw my issue is still open. I would like to read more feedbacks from contributors, especially about using data transformations / DTOs in order to make sonata friendly with DDD."}}
{"actor":{"display_login":"Arkemlar"}}