As reported in #19 on the related bug: #1558830: media_element_validate() has hardcoded $form_state values reference and does not work with unlimited

The error message shown when an unlimited cardinality file field using the media widget is left empty is merely " is required". The field label is not shown.

This is because the #title attribute is not added to the child elements in media_field_widget_form(), so media_element_validate() has no info to grab.

I've only looked into this briefly. I see two fairly simple options:

  1. Just pull the field label from the $form_state array (or field_get_instance)
  2. Set $element['#title'] in media_field_widget_form(), then hide the element title on those child form elements in media_element_process() (so the extra titles on the child elements don't display).

I'm not sure of the best solution. Core file field widget attaches the title to each child element which makes me think #2's likely to be the best approach (core file field widget also provides its own multiple values behavior handling but I don't think Media needs to do anything that complex). Perhaps there are other modules or parts of core that rely on the existence of the title attribute on these child elements which would make #2 preferable to the simpler #1.

Thoughts?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

slucero’s picture

Status: Active » Needs review
FileSize
1.09 KB

I've attached a patch to implement approach #2:

Set $element['#title'] in media_field_widget_form(), then hide the element title on those child form elements in media_element_process() (so the extra titles on the child elements don't display).

An opportunity for improvement on this might be to find a different method in media_element_process() to determine if the title should be hidden without storing the field cardinality in the element.

azinck’s picture

Little typo. Here's a fix.

Dave Reid’s picture

Revised patch that doesn't require any #cardinality to be passed into the media element, and only passes in #title_display.

crazybutable’s picture

Status: Needs review » Reviewed & tested by the community

This patch solves the issue for me.

Dave Reid’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.