I have set up a media image file field on a couple of content types, per the instructions given in the Media 2.x Quick Start Guide.

The image file field has a custom field I added (admin/structure/file-types/manage/image/fields). This field has a default value. I would like to be able to change the default value via a hook based on conditions determined at runtime.

While I have found that hook_form_alter is called when the Edit Image modal (AJAX) dialog is displayed (see diagram "Create a New Node and Upload an Image" in the above-referenced documentation page), I would also like to have the chance to alter the default value even if the user does not click on the "Edit" button. Also, information about the fields attached to the image file field are not populated when hook_form_alter is called for the node create form for the content type that contains the image file field.

I considered hook_node_save, but by the time this hook is called, the file field has already been saved, and I can't tell if the user visited the Edit Image modal dialog or not. For that matter, even changing #default_value in hook_form_alter will change the displayed value, even if the user has already visited the dialog before and selected a new, non-default value for the field.

Is there an elegant way to determine in hook_form_alter and hook_node_save that the user has already changed the value for the field? Alternately, is there an earlier hook that can adjust the default value for these fields before either of these hooks are called?

Update: I thought of a reasonable workaround. If I set the default value for the attached field to "I don't care", then I could calculate the dynamic default value at view time. I'd rather not have an "I don't care" value, though, and would therefore still be interested in having this question answered.

Update 2: I am now happily running with the "I don't care" workaround deployed on my live site. I would still be interested in an answer on how to elegantly set the default value for media file fields, though.

1 Answer 1


Drupal has the capability to dynamically set a default value for a field, but it is not exposed in the UI.

Each field instance can be set a 'default_value_function', which has the following signature:

function my_default_value_function($entity_type, $entity, $field, $instance, $langcode) {
  // Returns a standard field structure.
  return array(
    0 => array(
      // This can be different depending on the field type.
      'value' => 'myvalue',

To set the default value function, either:

  • Modify the field structure exported by Features to add a 'default_value_function' in the 'field_instance' part (and revert the feature),
  • Do something like this (once):

    $instance = field_info_instance($entity_type, $field_name, $bundle_name);
    $instance['default_value_function'] = 'my_default_value_function';
  • That's awesome. Thank you so much for the explanation. Mar 20, 2013 at 14:44
  • Stack Exchange expects me to wait 15 more hours before awarding the bounty. Mar 20, 2013 at 14:45

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.