way to count number of attached audio files?
anthonym - May 25, 2008 - 19:31
| Project: | Audio |
| Version: | 5.x-2.x-dev |
| Component: | audio_playlist |
| Category: | support request |
| Priority: | critical |
| Assigned: | Unassigned |
| Status: | active (needs more info) |
Jump to:
Description
1. Is there a way (function I could call) to count the number of audio files attached to a node (i.e. number of files in a playlist)?
2. I'd then like to use the result as the condition for an if statement in my template.php override to display the extended player for multiple files but switch to the slim player for a single file. Assuming #1 is possible, can anyone give me a clue as to the best way to accomplish #2?
My current theme override:
<?php
/**
* EXAMPLE THEME FUNCTION OVERRIDE TO DISPLAY ONE XSPF PLAYER
* Theme used if multiple files are attached
*/
function garland_audio_attach_list($nids, $teaser = FALSE) {
drupal_add_css(drupal_get_path('module', 'audio_attach') .'/audio_attach.css');
// generate an XSPF player for this playlist
$player = audio_get_players('name', variable_get('audio_feeds_default_player', 'xspf_extended'));
$playlist_url = url('node/' . arg(1) .'/xspf', NULL, NULL, TRUE);
$output .= "<strong>Audio samples:</strong><br>";
$output .= theme($player['theme_xspf'], $playlist_url);
$output .= "<br>";
foreach ($nids as $aid) {
$audio = node_load($aid);
$audio = node_prepare($audio, $teaser);
$title = $audio->status ? l($audio->title, 'node/' . $audio->nid, NULL, NULL, NULL, TRUE) : check_plain($audio->title);
$items[] = '<div class="title">'. $title .'</div>';
}
// removed individual players
// $output .= theme('item_list', $items, null, 'ol', array('class' => 'audio-attach-list'));
// commented above line to remove html playlist
return $output;
}
?>
#1
<?phpif (!empty($nids)) {
if (count($nids) > 1) {
// show multiple player
}
elseif (count($nids) == 1) {
// show single player
}
}
?>
#2
#3
Just wanted to add my current code incorporating zirafa's answer in case it might assist someone else looking for a similar solution. It works well. Thanks.
<?php
/**
* EXAMPLE THEME FUNCTION OVERRIDE TO DISPLAY ONE XSPF PLAYER
* Theme used if multiple files are attached
*/
function garland_audio_attach_list($nids, $teaser = FALSE) {
drupal_add_css(drupal_get_path('module', 'audio_attach') .'/audio_attach.css');
//if statement below counts number of files in playlist: returns extended player if > 1, slim player if = 1
if (is_array($nids) && count($nids) > 1) {
// generate an XSPF extended player for this playlist
$player = audio_get_players('name', variable_get('audio_feeds_default_player', 'xspf_extended'));
$playlist_url = url('node/' . arg(1) .'/xspf', NULL, NULL, TRUE);
$output .= "<strong>Audio samples:</strong><br>";
$output .= theme($player['theme_xspf'], $playlist_url);
$output .= "<br>";
} else {
// generate an XSPF slim player for this playlist
$player = audio_get_players('name', variable_get('audio_feeds_default_player', 'xspf_slim'));
$playlist_url = url('node/' . arg(1) .'/xspf', NULL, NULL, TRUE);
$output .= "<strong>Audio sample:</strong><br>";
$output .= theme($player['theme_xspf'], $playlist_url);
$output .= "<br>";
}
return $output;
}
?>
#4
I need a little more info on the above, please. I'm now working with a separate template for the node type which includes the audio players as defined above. I have this in my node-type.tpl.php file, but it only partly works:
<?phpprint garland_audio_attach_list($nids, $teaser = FALSE)
?>
I do get a player, but it appears on every node, whether or not there are audio files attached, and the switch I have configured to choose between the extended and slim players no longer works. Only the slim player gets displayed (although it does play multiple selections if they are in a playlist). I guess I need another way to count attached files in the if statement? Any ideas?
#5
You'll have to give a little more detail about what you are trying to do. But my guess is that the variable $nids isn't getting set. Now that you are dealing with a node-[type].tpl.php file, you have to make sure the variables are getting sent to the tpl.php file properly. Often times this is done with a phptemplate_callback function. But you may already have the info you need...Try doing
<?phpprint_r($node)
?>
#6
Zirafa, thanks for the reply. That helped a lot. I'm not sure if this is what you meant, but I changed my node template line to:
<?phpprint garland_audio_attach_list($node->audio_attach, $teaser = FALSE)
?>
which brought back the switching effect I was after, but the slim player was still appearing on all nodes. I'm guessing this was because (according to the devel module) the audio_attach array seemed to be present on all of my nodes even if the array was empty (i.e. no files attached). I solved this by wrapping the player generation/switching code in another if statement which checks to see if the array has anything in it first. I don't know how clean/efficient this is, but it's working so far. Included below for the record:
<?phpfunction garland_audio_attach_list($nids, $teaser = FALSE) {
drupal_add_css(drupal_get_path('module', 'audio_attach') .'/audio_attach.css');
if (count($nids) > 0) {
if (count($nids) > 1) {
// generate an XSPF extended player for this playlist
$player = audio_get_players('name', variable_get('audio_feeds_default_player', 'lacymorrow_xspf_player'));
$playlist_url = url('node/' . arg(1) .'/xspf', NULL, NULL, TRUE);
$output .= "<strong>Audio samples:</strong><br>";
$output .= theme($player['theme_xspf'], $playlist_url);
$output .= "<br>";
} else {
// generate an XSPF slim player for this playlist
$player = audio_get_players('name', variable_get('audio_feeds_default_player', 'lacymorrow_xspf_player_slim'));
$playlist_url = url('node/' . arg(1) .'/xspf', NULL, NULL, TRUE);
$output .= "<strong>Audio sample:</strong><br>";
$output .= theme($player['theme_xspf'], $playlist_url);
$output .= "<br>";
}
return $output;
}
}
?>
#7
Check my post #1 which I revised to use !empty($nids) instead of is_array($nids). That might work better because !empty($nids) should evaluate to FALSE if $nids => array() (empty array). Or at least that's what http://php.net/empty says. With is_array it returns TRUE even if it is an empty array.
But the way you've done it works too.