2

I have a non-required field_thumbnail that is of type "Multimedia asset", and the widget type is "Media file selector". However on the field's settings page there is no option to specify a default value. I've also perused the Administrative section seeking configuration in the Image styles, and File styles, but neither permit a default image to be used.

What would be the recommended way to get a default image, when a user does not select any media object for the given node?

1 Answer 1

3

Whenever I need to do this I just use a plain old Image field and use the Media File Selector as the widget. That way you can specify a default image for the field and still use the nice popup media browser.

So far I haven't found any benefits to using a Multimedia Asset field type over an Image field type in this situation, but that's not to say there aren't any or won't be any in the future.

EDIT

At the code level the following might help you get started:

// Implement hook_field_info_alter to add a setting for the multimedia field type
function MYMODULE_field_info_alter(&$info) {
  $info['media']['settings'] = array(
    'default_asset' => NULL
  );
}

// Implement hook_field_settings_form on behalf of the media module
function media_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  $form['default_asset'] = array(
    '#type' => 'media',
    '#title' => 'Default Multimedia Asset',
    '#description' => 'The default asset to use when none is selected',
    '#default_value' => $settings['default_asset']
  );

  return $form;
}

That will add a default multimedia asset picker to the field settings. Then you just need to work out how to inject that default value into the field display when it's otherwise empty. I would guess at the use of something like hook_field_prepare_view() for this.

Note that there's no way to restrict the type of the default multimedia asset as those types are set at a per instance level, not at the field level. If you want to do it at the instance level much of the same code above will help but you'll have to extend it further to restrict the type of media based on the checkboxes on the instance form.

2
  • That's a worthy solution, that actually works in my specific case (thumbnails are rarely anything other than images) but it truly doesn't solve the underlying issue here. I wouldn't be opposed to a code level solution to this problem, if someone had something to offer. I'll leave it open for the time being to see if others have any other ideas. Apr 5, 2012 at 15:29
  • Update to previous comment ^: Nice effort elaborating on a solution. I'll go ahead and award you some points here for that effort, tho as I said above for my specific case i was actually able to get away with using the Image widget... Maybe this will help someone else. Apr 10, 2012 at 21:50

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.