My content type has an image field. I'd like to propose to the editor two possible displays for that uploaded image, either 100% width or 50% width How can i achieve that?

EDIT: i found out this module : https://www.drupal.org/project/field_image_style Is this module stable enough?

How can I make this behavior compatible with the field formatter from colorbox module?


You could add a separate select dropdown textfield with two options, which are the image styles you'd like, and if they select the alternate one, update the field in hook_entity_view.

 function mymodule_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
   if ($entity->getEntityType()->id() == 'node' && $entity->bundle() == 'article' && !$entity->field_image_style->isEmpty()) {
     $style = $entity->field_image_style->getValue()[0]['value'];
     $build['field_image'][0]['#image_style'] = $style;
| improve this answer | |
  • mymodule_entity_view is not fired in my case when loading /node/nid !? – Matoeil Nov 9 '16 at 11:52
  • 1
    it definitely fires on node pages. Try clearing cache, or to develop without the render cache on, put this in settings.php: $settings['cache']['bins']['render'] = 'cache.backend.null'; – oknate Nov 9 '16 at 14:38
  • it is fired when function mymodule_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {. is that because the copy paste makes the ide not adding the necessary use ... line? – Matoeil Nov 14 '16 at 10:25
  • I think it may be due to render caching. Do you have render cache turned off? – oknate Nov 14 '16 at 13:01
  • $settings['cache']['bins']['render'] = 'cache.backend.null'; generate an error. i enter the hook function if i write as my comment above. The style is not applied though, is it the machine name to assign in $style? – Matoeil Nov 14 '16 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.