Error:

Notice: Undefined index: field_testimonial_image in media_element_validate() (line 990 of C:\wamp\www\presence\1530chestnut.com\sites\all\modules\contrib\media\media.module).
Warning: Invalid argument supplied for foreach() in media_element_validate() (line 990 of C:\wamp\www\presence\1530chestnut.com\sites\all\modules\contrib\media\media.module).

Perceived Reason:

This is relevant to Media Fields that are marked as "required" when they are attached to a Field Collection. The function mentioned above - media_element_validate() - checks to ensure that an FID is actually present and when it does so, it looks directly for the field_name in $form_states['values']. This structure does not hold for cases where the Media Field is in a Field Collection.

Possible Solution (I dunno how to make patches, apologies for that...):

In the case of Field Collections, the element's $element['#field_parents'] is fleshed out with the chain of parents that lead to the field. In regular fields, $element['#field_parents'] is empty. I've added my code suggestion below but feel it can be optimised by someone more experienced and turned into a patch that can be pulled into the project. The expertise of someone adept at Field Collections would be most appreciated to ensure that this code covers all bases for this scenario.

Suggested Code:

FROM----------------------------------------------------------------------------

function media_element_validate(&$element, &$form_state) {
  if ($element['#required']) {
    $field_name = $element['#field_name'];
    $lang = $element['#language'];

    $has_value = FALSE;
    foreach ($form_state['values'][$field_name][$lang] as $value) {
      if ($value['fid']) {
        $has_value = TRUE;
      }
    }
    if (!$has_value) {
      form_error($element, t('%element_title is required.', array('%element_title' => $element['#title'])));
    }
  }
}

TO------------------------------------------------------------------------------

function media_element_validate(&$element, &$form_state) {
  if ($element['#required']) {
    $field_name = $element['#field_name'];
    $lang = $element['#language'];

    $has_value = FALSE;
    if(count($element['#field_parents'])) {
      $values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);

      foreach ($values[$field_name][$lang] as $value) {
        if ($value['fid']) {
          $has_value = TRUE;
        }
      }
    } else {
      foreach ($form_state['values'][$field_name][$lang] as $value) {
        if ($value['fid']) {
          $has_value = TRUE;
        }
      }
    }
    if (!$has_value) {
      form_error($element, t('%element_title is required.', array('%element_title' => $element['#title'])));
    }
  }
}

============================================

Many, many, many thanks to the maintainers of Media module and Field Collections module!

Comments

ParisLiakos’s picture