{"action":"created","issue":{"url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/1257","repository_url":"https://api.github.com/repos/sonata-project/SonataMediaBundle","labels_url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/1257/labels{/name}","comments_url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/1257/comments","events_url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/1257/events","html_url":"https://github.com/sonata-project/SonataMediaBundle/issues/1257","id":231617055,"number":1257,"title":"MediaType ignores (CSS-)class attribute in template (media_widgets.html.twig)","user":{"login":"moritzwachter","id":15142607,"avatar_url":"https://avatars3.githubusercontent.com/u/15142607?v=3","gravatar_id":"","url":"https://api.github.com/users/moritzwachter","html_url":"https://github.com/moritzwachter","followers_url":"https://api.github.com/users/moritzwachter/followers","following_url":"https://api.github.com/users/moritzwachter/following{/other_user}","gists_url":"https://api.github.com/users/moritzwachter/gists{/gist_id}","starred_url":"https://api.github.com/users/moritzwachter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/moritzwachter/subscriptions","organizations_url":"https://api.github.com/users/moritzwachter/orgs","repos_url":"https://api.github.com/users/moritzwachter/repos","events_url":"https://api.github.com/users/moritzwachter/events{/privacy}","received_events_url":"https://api.github.com/users/moritzwachter/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-26T12:46:28Z","updated_at":"2017-05-26T15:03:42Z","closed_at":null,"body":"### Environment\r\n\r\n#### Sonata packages\r\n\r\n```bash\r\n$ composer show sonata-project/*\r\nsonata-project/admin-bundle              3.16.0 The missing Symfony Admin Generator\r\nsonata-project/block-bundle              3.3.2  Symfony SonataBlockBundle\r\nsonata-project/cache                     1.0.7  Cache library\r\nsonata-project/cache-bundle              2.3.1  This bundle provides caching services\r\nsonata-project/classification-bundle     3.3.0  Symfony SonataClassificationBundle\r\nsonata-project/core-bundle               3.3.0  Symfony SonataCoreBundle\r\nsonata-project/datagrid-bundle           2.2.1  Symfony SonataDatagridBundle\r\nsonata-project/doctrine-extensions       1.0.2  Doctrine2 behavioral extensions\r\nsonata-project/doctrine-orm-admin-bundle 3.1.4  Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle\r\nsonata-project/easy-extends-bundle       2.1.10 Symfony SonataEasyExtendsBundle\r\nsonata-project/exporter                  1.7.1  Lightweight Exporter library\r\nsonata-project/formatter-bundle          3.2.1  Symfony SonataFormatterBundle\r\nsonata-project/google-authenticator      1.1.0  Library to integrate Google Authenticator into a PHP project\r\nsonata-project/intl-bundle               2.3.0  Symfony SonataIntlBundle\r\nsonata-project/media-bundle              3.5.1  Symfony SonataMediaBundle\r\nsonata-project/notification-bundle       3.1.0  Symfony SonataNotificationBundle\r\nsonata-project/page-bundle               3.4.1  This bundle provides a Site and Page management through container and block services\r\nsonata-project/seo-bundle                2.1.0  Symfony SonataSeoBundle\r\nsonata-project/timeline-bundle           3.1.0  Integrates SpyTimelineBundle into Sonata\r\nsonata-project/user-bundle               3.2.3  Symfony SonataUserBundle\r\n\r\n```\r\n\r\n#### Symfony packages\r\n\r\n```bash\r\n$ composer show symfony/*\r\nsymfony/assetic-bundle     v2.8.1             Integrates Assetic into Symfony2\r\nsymfony/monolog-bundle     v3.1.0             Symfony MonologBundle\r\nsymfony/phpunit-bridge     dev-master 9706535 Symfony PHPUnit Bridge\r\nsymfony/polyfill-apcu      v1.3.0             Symfony polyfill backporting apcu_* functions to lower PHP versions\r\nsymfony/polyfill-intl-icu  v1.3.0             Symfony polyfill for intl's ICU-related data and classes\r\nsymfony/polyfill-mbstring  v1.3.0             Symfony polyfill for the Mbstring extension\r\nsymfony/polyfill-php54     v1.3.0             Symfony polyfill backporting some PHP 5.4+ features to lower PHP versions\r\nsymfony/polyfill-php55     v1.3.0             Symfony polyfill backporting some PHP 5.5+ features to lower PHP versions\r\nsymfony/polyfill-php56     v1.3.0             Symfony polyfill backporting some PHP 5.6+ features to lower PHP versions\r\nsymfony/polyfill-php70     v1.3.0             Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions\r\nsymfony/polyfill-util      v1.3.0             Symfony utilities for portability of PHP codes\r\nsymfony/security-acl       v3.0.0             Symfony Security Component - ACL (Access Control List)\r\nsymfony/swiftmailer-bundle v2.5.4             Symfony SwiftmailerBundle\r\nsymfony/symfony            v2.8.18            The Symfony PHP framework\r\n```\r\n\r\n#### PHP version\r\n\r\n```bash\r\n$ php -v\r\nPHP 7.0.8-3+deb.sury.org~trusty+1 (cli) ( NTS )\r\nCopyright (c) 1997-2016 The PHP Group\r\nZend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies\r\n    with Zend OPcache v7.0.8-3+deb.sury.org~trusty+1, Copyright (c) 1999-2016, by Zend Technologies\r\n```\r\n\r\n## Subject\r\nWhen trying to set a CSS class to a `MediaType` I experienced that the attribute `class` is completely ignored by the form type i.e. it's never inserted in the widget's template.\r\n\r\n(see [`media_widgets.html.twig:21-31`](https://github.com/sonata-project/SonataMediaBundle/blob/3.x/Resources/views/Form/media_widgets.html.twig#L21-L31)):\r\n```twig\r\n        <div class=\"col-md-4 pull-left\">\r\n            {% if value is not empty and value.providerReference %}\r\n                <div class=\"pull-left\" style=\"margin-right: 5px\">\r\n                    {% thumbnail value, 'admin' with {'class': 'img-polaroid media-object'} %}\r\n                </div>\r\n            {% else %}\r\n                <div class=\"pull-left\" style=\"margin-right: 5px\">\r\n                    <img src=\"{{ asset('bundles/sonatamedia/grey.png') }}\" class=\"img-polaroid media-object\" style=\"width: 85px; height: 85px\"/>\r\n                </div>\r\n            {% endif %}\r\n        </div>\r\n```\r\nI am kind of new to the Sonata community so maybe there's a reason why it is ignored there. \r\nBut it works for 'normal' form widgets and I thought it's not that big of an edge case to put a class on an `<img>`-tag :wink: \r\n\r\nMy workaround was to overwrite the template as follows:\r\n(again `media_widgets.html.twig:21-31`):\r\n```twig\r\n        <div class=\"col-md-4 pull-left\">\r\n            {# to avoid Symfony's 'Variable \"class\" does not exist.' error #}\r\n            {% if attr.class is defined %}\r\n                {%  set cssClass = attr.class %}\r\n            {% else %}\r\n                {% set cssClass = '' %}\r\n            {% endif %}\r\n\r\n            {% if value is not empty and value.providerReference %}\r\n                <div class=\"pull-left\" style=\"margin-right: 5px\">\r\n                    {% thumbnail value, 'admin' with {'class': cssClass ~ ' img-polaroid media-object'} %}\r\n                </div>\r\n            {% else %}\r\n                <div class=\"pull-left\" style=\"margin-right: 5px\">\r\n                    <img src=\"{{ asset('bundles/sonatamedia/grey.png') }}\" class=\"{{ cssClass }} img-polaroid media-object\" style=\"width: 85px; height: 85px\"/>\r\n                </div>\r\n            {% endif %}\r\n        </div>\r\n```\r\n\r\n## Steps to reproduce\r\n- Create a new MediaType form field\r\n- add the attr => class Configuration as follows: \r\n```php\r\n$builder->add('image', MediaType::class, [\r\n                    'provider' => 'sonata.media.provider.image',\r\n                    'context' => 'default',\r\n                    'label' => false,\r\n                    'enable_upload' => false,\r\n                    'attr' => [\r\n                        'class' => 'i-want-to-add-this-class'\r\n                    ]\r\n                ]\r\n            )\r\n```\r\n\r\n## Expected results\r\nThe page's HTML should contain the CSS class `i-want-to-add-this-class` on the `<img>`-tag.\r\n\r\n## Actual results\r\nNo change in the markup.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/comments/304306550","html_url":"https://github.com/sonata-project/SonataMediaBundle/issues/1257#issuecomment-304306550","issue_url":"https://api.github.com/repos/sonata-project/SonataMediaBundle/issues/1257","id":304306550,"user":{"login":"greg0ire","id":657779,"avatar_url":"https://avatars2.githubusercontent.com/u/657779?v=3","gravatar_id":"","url":"https://api.github.com/users/greg0ire","html_url":"https://github.com/greg0ire","followers_url":"https://api.github.com/users/greg0ire/followers","following_url":"https://api.github.com/users/greg0ire/following{/other_user}","gists_url":"https://api.github.com/users/greg0ire/gists{/gist_id}","starred_url":"https://api.github.com/users/greg0ire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/greg0ire/subscriptions","organizations_url":"https://api.github.com/users/greg0ire/orgs","repos_url":"https://api.github.com/users/greg0ire/repos","events_url":"https://api.github.com/users/greg0ire/events{/privacy}","received_events_url":"https://api.github.com/users/greg0ire/received_events","type":"User","site_admin":false},"created_at":"2017-05-26T15:03:42Z","updated_at":"2017-05-26T15:03:42Z","body":"We should probably allow let people add any html attribute, not just css class. For instance, there is no `alt` attribute, and if you have a class, you might want to make the `style` attribute empty. Would you be willing to provide a PR for this?"}}
{"actor":{"display_login":"greg0ire"}}