Question

I have a content type "Audio" which includes an audio file field. This field accepts multiple values, so each node could contain any number of audio files. Ordinarily, when you build an RSS feed view, it will generate one <item> per node, and each of those will have multiple <enclosure> entries, one per audio file. But for my purposes I need the view to output a separate <item> for each of the audio file values. So if, for example, there are 3 audio files in a selected node, the whole node will be repeated in the feed 3 times, and each one will contain only one audio file enclosure.

The reason for this is that in my specific case, I'm using hook_node_view() in a custom module to add MRSS tags to each of the nodes that the view generates. So rather than the audio files being wrapped by <enclosure> they'll be wrapped in <media:content> tags. According to the MRSS specification, you shouldn't have more than one <media:content> entry per <item> unless they represent the same content. This is because the other tags like <title> and <description> pertain to all of the <media:content> tags in the same <item>. Ironically enough, the customization hook is the easiest part, but first I need to get the view to output the nodes in the manner I've described.

So is there some way I can use the new Drupal 7 views grouping feature or some other method to generate the output I'm looking for? I'd love to know the trick! Otherwise I'll have to use a custom menu entry and lose all the advantages (such as automatic caching and being able to work within the Views UI) that I get with Views.

Was it helpful?

Solution

Create a view of "files" and then under the Advanced collapsed fieldset on the right side of the Views UI, create a relationship (a join) of the node with the file.

Here's a quick view you can copy here and import into your views ui:

<?php
    $view = new view;
    $view->name = 'test';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'file_managed';
    $view->human_name = 'test';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'test';
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Relationship: File Usage: Content */
    $handler->display->display_options['relationships']['file_to_node']['id'] = 'file_to_node';
    $handler->display->display_options['relationships']['file_to_node']['table'] = 'file_usage';
    $handler->display->display_options['relationships']['file_to_node']['field'] = 'file_to_node';
    $handler->display->display_options['relationships']['file_to_node']['required'] = 0;
    /* Field: File: Name */
    $handler->display->display_options['fields']['filename']['id'] = 'filename';
    $handler->display->display_options['fields']['filename']['table'] = 'file_managed';
    $handler->display->display_options['fields']['filename']['field'] = 'filename';
    $handler->display->display_options['fields']['filename']['label'] = '';
    $handler->display->display_options['fields']['filename']['alter']['alter_text'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['make_link'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['absolute'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['word_boundary'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['ellipsis'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['strip_tags'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['trim'] = 0;
    $handler->display->display_options['fields']['filename']['alter']['html'] = 0;
    $handler->display->display_options['fields']['filename']['hide_empty'] = 0;
    $handler->display->display_options['fields']['filename']['empty_zero'] = 0;
    $handler->display->display_options['fields']['filename']['link_to_file'] = 1;
    /* Field: Content: Body */
    $handler->display->display_options['fields']['body']['id'] = 'body';
    $handler->display->display_options['fields']['body']['table'] = 'field_data_body';
    $handler->display->display_options['fields']['body']['field'] = 'body';
    $handler->display->display_options['fields']['body']['relationship'] = 'file_to_node';
    /* Field: Content: Date */
    $handler->display->display_options['fields']['field_date']['id'] = 'field_date';
    $handler->display->display_options['fields']['field_date']['table'] = 'field_data_field_date';
    $handler->display->display_options['fields']['field_date']['field'] = 'field_date';
    $handler->display->display_options['fields']['field_date']['relationship'] = 'file_to_node';
    $handler->display->display_options['fields']['field_date']['alter']['alter_text'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['make_link'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['absolute'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['external'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['replace_spaces'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['trim_whitespace'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['nl2br'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['word_boundary'] = 1;
    $handler->display->display_options['fields']['field_date']['alter']['ellipsis'] = 1;
    $handler->display->display_options['fields']['field_date']['alter']['strip_tags'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['trim'] = 0;
    $handler->display->display_options['fields']['field_date']['alter']['html'] = 0;
    $handler->display->display_options['fields']['field_date']['element_label_colon'] = 1;
    $handler->display->display_options['fields']['field_date']['element_default_classes'] = 1;
    $handler->display->display_options['fields']['field_date']['hide_empty'] = 0;
    $handler->display->display_options['fields']['field_date']['empty_zero'] = 0;
    $handler->display->display_options['fields']['field_date']['hide_alter_empty'] = 1;
    $handler->display->display_options['fields']['field_date']['settings'] = array(
      'format_type' => 'long',
      'fromto' => 'both',
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_to' => '',
      'show_repeat_rule' => 'show',
    );
    $handler->display->display_options['fields']['field_date']['group_rows'] = 1;
    $handler->display->display_options['fields']['field_date']['delta_offset'] = '0';
    $handler->display->display_options['fields']['field_date']['delta_reversed'] = 0;
    $handler->display->display_options['fields']['field_date']['delta_first_last'] = 0;
    $handler->display->display_options['fields']['field_date']['field_api_classes'] = 0;
    /* Field: Content: Link */
    $handler->display->display_options['fields']['view_node']['id'] = 'view_node';
    $handler->display->display_options['fields']['view_node']['table'] = 'views_entity_node';
    $handler->display->display_options['fields']['view_node']['field'] = 'view_node';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    /* Sort criterion: File: Upload date */
    $handler->display->display_options['sorts']['timestamp']['id'] = 'timestamp';
    $handler->display->display_options['sorts']['timestamp']['table'] = 'file_managed';
    $handler->display->display_options['sorts']['timestamp']['field'] = 'timestamp';
    $handler->display->display_options['sorts']['timestamp']['order'] = 'DESC';

    /* Display: Page */
    $handler = $view->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'test';

?>

As for the correct markup surrounding the entire RSS printout, you can manipulate the views templates. To modify each record, use views-view-fields--test.tpl.php (for example). And for the wrapper around the entire view, use views-view--test.tpl.php (for example). The templates available are under the "Themes" link at the bottom of the third rail (the collapsed one) on the Views UI. Just drop the views template files into your theme's folder and drush cc all.

Joe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top