5

I use the Media (ver, 2) module to manage a library of media assets. It allows reuse of the same media in several nodes and keeps track of media use.

When reusing media (e.g.) images, I want to be able to add different captions (and maybe other metadata) to each use of the same image. I.e.: When I use the same image attached to two different nodes, I want to set the caption for each instance.

There is a great tutorial “How to Add Captions to Images in Drupal” about adding captions – but its approach is to add the caption to the image entity itself. This means that the same caption must be repeated whenever the image is reused. I want an unique caption for each use instance.

I’ve noticed that version 2 (7.x-2.0-alpha4) of the Media module manage some metadata (e.g. field_media_description) about each media instance use in the two tables {field_data_field_media} and {field_revision_field_media}. I’ve tried to use the already existing field field_media_description for the caption. This works, and almost does what I want.

However, I want the caption field to be a textarea (not a textfield), and I prefer not to overload some existing field with new functionality (maybe breaking the initial intent for this existing field) - so I want to add another field (field_media_caption) to these tables.

I’ve been digging about the Media module for days, trying to figure out how to add my own fields to these tables, and have not been able to figure it out.

So:
How do I programatically add additional metadata fields to the Media module's field tables?

I will also accept an alternative solution that allows me to use a different module to manage a library of reusable images and add captions fields per use-instance.

1
  • 1
    This looks actually very straight forward with some field collection and I'm sure you have thought of it and opted out but anyways, I wanted to mention.
    – duru
    Apr 19, 2015 at 21:29

3 Answers 3

1

There is a sandbox module I helped with, Media Wrapper, which solves this problem by managing media via a field collection. This has the advantage of making it possible to associate any fields you like with a media file, including captions. And the metadata is all per-use rather than per-file.

As it is a sandbox module, it's not as polished as we would like but it does a reasonable job of solving this problem.

To use it, once installed you create a field collection with your media field and meta data and then configure it via field settings of the field collection to say how each field is to be used (for alt text, title, caption etc).

4
  • Thanks for replying! I am going to check out Media Wrapper unless I get an answer that tells me how to do this directly in the Media module. Apr 18, 2015 at 9:42
  • Great. One thing I forgot was to make sure you checkout the 7.x.2.x branch. That's the one under active development and it's much different from the earlier work on this module. Apr 19, 2015 at 12:07
  • Yes. As you can see from my question I am using ver. 2 (i.e. the 7.x-2.x branch). It is a little shaky, but its reuse of the core file type is a much saner approach. Apr 19, 2015 at 12:24
  • My comment referred to Media Wrapper. Weird that it makes perfect sense with respect to Media as well. Apr 19, 2015 at 13:13
1
+200

I know your question specifically aims at the media module but still I would like to point you in another direction, since it really was an eye opener for me:

As far as I know the scald module can basically do everything the media module can, and more. And the feature your asking for is already built in: standard captions (generated from the media item's title) with the option to override the caption on each media instance use.

  • fieldable media 'atoms'

  • drag'n'drop media into rich text areas and/or media reference fields

  • central asset management

  • extendable – can handle images, files, video, galleries

  • scalable – used by large publishing houses

  • drupal 8 branch under way

Here is some details available in case you want to go all the way and migrate to scald.

And here is a series of videos that can be helpful to get started.

3
  • The scald contexts (structure > scald > contexts) take care of image styles / image filters. For each editor instance you can define a default context – and it's basically the same for the reference fields. Only difference is that you can then either use the output settings to define the image style or use the function scald_atom_load($sid) to apply an image filter. It is actually a bit complex – I followed the installation guidelines given on the module's page and after a while I got it running. And I love it. Apr 20, 2015 at 16:21
  • None of the answers did actually answer my question. However, as far as alternative answers go, both Alfred Armstrongs suggestion of using the Media Wrapper field collection, and yours suggestion av using Scald, were helpful workaround suggestions. It is almost a toss-up, but I've decided to let you have the bounty. I've started converting all the media assets to Scald. It solves my caption problem nicely, and also a number of other problems I've had with the Media module. Thanks for bringing it to my attention! Apr 23, 2015 at 11:32
  • Oh wow thanks! And great to hear we have another scald user around… Apr 23, 2015 at 11:42
0

I am not sure if you have seen that the Media module (with File Entity) opens up File Types in the Drupal admin. From here, you can define new types and field them.

Off the top of my head, without adding code, you may be able to try and attach metadata via Field Collections to a file type.

For example, a collection that holds caption, title, alt fields, and has a node reference field. Set it to unlimited values.

For the display, I would try creating a view block that shows the image and attach the field collection data on it filtered by node reference ID by the current context of the given node page.

That would most likely be my first approach to adding contextual data to a single media entity without doing a lot of code.

Your Answer

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

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