2

XY problem: I need to extend the default image widget in the entity edit form with some custom fields so that, per image, e.g. a copyright and description, independently from the alt and title field, can be set.

I can add the fields by altering the core image module files, and by manually adding the respective database columns. But of course, I would like to alter the plugins with a custom module.

I was able to create a custom formatter based on the ImageWidget as described in this tutorial.

My question is: is there a way to alter the default image widget/ field type to add custom fields rather than defining custom formatters/ widgets?

10
  • 2
    I think you have a conceptual gag here in your concept. You want to make the image field to store new values (copyright, description). Widget is for the display. It won't able to display things, which doesn't exist in the field type. I would suggest you to rather create a content type for this (image field with two text fields) or even better a "weak entity", which can be provided by paragraph module.
    – ssibal
    Aug 2, 2016 at 11:17
  • well, can I then extend the field type without implementing my own? :)
    – Alex
    Aug 2, 2016 at 13:00
  • Sure! You can extend the original field type, but you will have to rewrite some part of the @annotations (field type id for example). And then you also will have to extend the widget, to display the new values. There are some HOWTO-s on drupal.org. Just search for 'create custom field type drupal 8'.
    – ssibal
    Aug 2, 2016 at 13:38
  • @ssibal i found out i can just use the existing plugin id in the annotations. is this bad practice or good?
    – Alex
    Aug 2, 2016 at 13:51
  • Using the existing plugin id will mean you override the default (just because when the system parsing the plugin types your module is later I guess). In general I would always leave core things as they are, even from UI perspective. I can imagine a complex system, where you don't want to list elements which are never used, but in your case I would use a different ID, if that is possible.
    – ssibal
    Aug 2, 2016 at 13:54

2 Answers 2

3

As an alternative, more flexible way, you could chose to use either File entity or Media entity, both give you a fieldable entity that you can add your additional fields, without having to write a single line of custom code.

File entity makes the files themself fieldable and adds file types, so images can have different fields than videos for example.

Media entity adds a separate entity that you work with, and works well combined with a number of other modules like Entity Browser and Inline Entity Form. There are some good tutorials out there:

http://www.annertech.com/blog/reusable-media-drupal-8-tutorial http://flocondetoile.fr/blog/managing-media-drupal-8

1
  • I know those modules, but they dont quite come up with what I need. The file entity module for instance does not show thumbnails. Also, once installed and uninstalled it seems the module is not working properly...
    – Alex
    Aug 3, 2016 at 7:20
1

I needed to do something similar to this in Drupal 8. I wanted to add a description field to the image field so that it could be displayed for each image.

I couldn't find a solution, so I ended up creating a custom module to do this. Ended up creating a custom field type, custom field widget and custom field formatter. Also using hook theme, a .field.inc to provide the variables for a template, and finally the said template.

This is one of the first pages I came across on Google searching for this, so I thought I'd add it in case it helps someone else.

https://github.com/WiddleWabbit/Drupal-ExtendedImage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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