Bellow is proof of concept code allowing a media audio field to be configured as a RSS Enclosure. To activate the formatter, the field display for the Media field is set to 'RSS Enclosure' under Manage Display / RSS. It worked on a Drupal 7 test instance; my iTunes client picked up a MP3 file from the feed. It also appears to work in a Views feed.

The code needs more work to get to production quality, if it's even the right way to implement RSS enclosures. I'm willing to continue working on it if it is the right way to go, or letting someone more capable take the lead. If it isn't the right way to go, could someone suggest what is?

<?php

/**
 * Implementation of hook_field_formatter_info().
 */
function fieldenc_field_formatter_info() {
  return array(
    'fieldenc_rss_enclosure' => array(
      'label' => t('RSS enclosure'),
      'field types' => array('media'),
    ),
  );
}

/**
 * Implementation of hook_field_formatter_view()
 */
function fieldenc_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if ($display['type'] == 'fieldenc_rss_enclosure') {
    foreach ($items as $delta => $item) {
      $entity->rss_elements[] = array(
        'key' => 'enclosure',
        'value' => '',
        'attributes' => array(
          'url' => file_create_url($item->uri),
          //'length' => '555',
          'type' => $item->filemime,
        ),
      );
    }
  }
}
?>

I've create a sandbox with a module implementing the code: http://drupal.org/sandbox/dmcgladdery/1190040

Comments?

Comments

dale42’s picture

Not perhaps clear from initial post, by working on it I mean submit as a patch to Media module.

Dave Reid’s picture

Note that a full project already implements this for media fields as a formatter: http://drupal.org/project/rss_field_formatters

dale42’s picture

Thanks for the heads up, Dave. This was exactly what I was looking for and couldn't find; it doesn't fare well with the search terms I was using.

dale42’s picture

Status: Active » Closed (fixed)