hi all!

i've been searching for the last hour, and havent been able to find anything too similar to this, so i figured i should ask for some help.

i'm building a band website for a friend. one of the content types is called "record," for each record they put out. now, bands release albums in different formats, so one of the fields i created was a multi value check box called "format." the available "formats" are CD, VINYL, CASSETTE (yes, some labels still put out cassettes!) and digital downloads.

in Views, i created a field view that lists the available format.

this is what it looks like:
CD
VINYL
CASSETTE

but this is what i'd LIKE it to look like:
CD/VINYL/CASSETTE

so to sum up, i'd like to group multiple value fields into a single row, and insert slashes or comma's between the values if there is more than 1.

Comments

markus_petrux’s picture

Status:Active» Fixed

I think you can override the function theme_content_view_multiple_field() in your theme's template.php. This funcion is located at the bottom of cck/includes/views/content.views.inc

Here you can check for the field name, and generate a different layout when multiple items are involved for a particular field.

hunterchristy’s picture

thanks! i got them all on the same line, but now it looks like

CDVINYLCASSETTE

how would i go about intelligently inserting slashes?

markus_petrux’s picture

Try with something like this:

<?php
function mytheme_content_view_multiple_field($items, $field, $values) {
  if (
$field['field_name'] != 'field_my_tuned_multivalue_field') {
   
// If this is not my tuned field, then use original theme function directly.
   
return theme_content_view_multiple_field($items, $field, $values);
  }

 
// Separate item values with my favorite separator.
 
$separator = ', ';
 
$array = array();
  foreach (
$items as $item) {
    if (!empty(
$item) || $item == '0') {
     
$array[] = $item;
    }
  }
  return
'<div class="field-item">'. implode($separator, $array) .'</div>';
}
?>
hunterchristy’s picture

so i got this back:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'theme_content_view_multiple_field' was given in C:\wamp\www\library\includes\theme.inc on line 617.

hunterchristy’s picture

a-ha! got it! thanks so much for your help.

<?php
function theme_content_view_multiple_field($items, $field, $values) {
 
$output = '';
 
// Separate item values with my favorite separator.
 
$separator = ', ';
 
$array = array();
  foreach (
$items as $item) {
    if (!empty(
$item) || $item == '0') {
     
$array[] = $item;
    }
  }
  return
'<span class="field-item">'. implode($separator, $array) .'</span>';
}
?>

though i AM curious as to what this means and what i'm missing out on by it not working in this function i cobbled together:
" // If this is not my tuned field, then use original theme function directly."

gafir777’s picture

I don't know if you guys noticed, but the $items array is still outputting the selected allowed values wrapped around a span:

<span id="thmr_7" class="thmr_call">My value 1</span>
<span id="thmr_8" class="thmr_call">My value 2</span>

I just want to get the raw value (the raw text) without the span around it. I'm not sure how to proceed for that.

Thanks

gafir777’s picture

Well I have added inside the foreach loop

$item = strip_tags($item);

It's not great but it works. I'm still trying to find a way to have the raw text inside the $items array rather than this html with span. Any idea?

markus_petrux’s picture

Re: Any idea?

Yes, here's where you would have to use a different formatter for the field. Note that you're playing here with a theme function that simply contatenates items for multiple value fields, but the individual items are formatted with the options selected for the field in the view settings form for that field.

gafir777’s picture

Hi Markus, THanks for your answer! So if I were to output it without the span, where do I change the option for the field? Is this directly from the Views interface? Or is it something I should be doing in the views template php for that particular field?

Thanks!

gafir777’s picture

Inside the settings for the field inside my view, i've changed from Format: default to Format: plain text

But it stills outputs the span.

Also tried to changed the display field settings for the content type, although I don't think it changes anything to the view...

markus_petrux’s picture

Not sure which fields it is, but each item is being formatted as per the format settings defined for the field in the view itself.

Now, find out the script used by this field to render the output for the formatter you have selected. You should find a theme function that you should be able to override from your theme.

That's a method. Another method would be that you can create a new formatter for this field. See CCK Formatters module for an example. Once you extend a field with a new formatter you can render it without any further HTML tags, and you will be able to choose this formatter from your view, or from the "Display fields" screen, etc.

gafir777’s picture

Hi markus !

Thanks for the advice. I think creating a new formatter is the way to go.

I'll keep you updated.

Thanks!

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jweedman’s picture

Thanks very much for this code. You saved me alot of time... and probably some hair from being pulled out. I would think Views could incorporate some sort of separator for multiple value fields.

Anyway, Thanks for sharing your solution... it really helped.

-jweedman

my-family’s picture

#3 code works well, but not with Ajax and exposed filters (with ajax in view, after applying the filter, the output seems as if the function was not used at all). Any ideas?

Anyway, thank you very much for the code!

syakely’s picture

I like it. One modification would be to call out the field you want to alter, so all other fields get rendered the stock way.

<?php
function mytheme_content_view_multiple_field($items, $field, $values) {
 
//drupal_set_message($field['field_name']);
 
if ($field['field_name'] == 'field_myfield' ) {
   
$output = '';
   
// Separate item values with my favorite separator.
   
$separator = ', ';
   
$array = array();
    foreach (
$items as $item) {
      if (!empty(
$item) || $item == '0') {
       
$array[] = $item;
      }
    }
    return
'<span class="field-item">'. implode($separator, $array) .'</span>';
  }
  else {
   
//else use the default function loacated at the bottom of cck/includes/views/content.views.inc
   
return theme_content_view_multiple_field($items, $field, $values);
  }
}

?>
redhatmatt’s picture

And no spelling errors either! Great Job Shane!

matsjacobsson’s picture

This works great for what I need..

But how do you do if you want it to affect the field even if it only contains one item? Thanks! /Mats

Found it! : http://drupal.org/node/472996#comment-3770168

matsjacobsson’s picture

Is there any way to limit it to a page display instead of field? /Mats

tarzadon’s picture

This is exactly what I was looking for. Thanks!

squarecandy’s picture

Thanks - perfect.

baisong’s picture

I had the same problem, creating a downloadable CSV feed view for a view, all my data was being concatenated with no separator -- I believe if it wasn't a plain-text view handler, there would be markup to separate the values.

As a solution, I used a customfield PHP snippet to generate arrays and implode them in the view. Probably slow for production sites, but for data extraction, it seemed sufficiently fast. Just install the customfield module, add a customfield PHP snippet (make sure you have permission to evaluate the PHP input format) and then paste these snippets in (along with the unseparated multivalued fields, exclude to hide them) to generate the separated list.

This is for text/number values

<?php
/**
 * make a list of all values in a multivalued field
 *
 * load the value for each value of the multivalued field,
 * put all of the values into an array, and then implode the
 * list for comma-separated display.
 *
 * to fish for more available variables, use `print_r($data);`
 */
$output = array();
foreach (
$data->node_data_field_my_text_field_my_text_value as $my_text) {
    if (
$my_text['value']) {
       
$output[] = $my_text['value'];
    }
}
print_r(implode($output,","));
?>

This is for node references (I needed the title from the node id (NID)

<?php
/**
 * make a list of titles of a referenced node field
 *
 * load the NID for each referenced node, then load the node
 * using the Drupal API, and create an array of titles from
 * the node object, then implode the list for comma-separated display.
 *
 * to fish for more available variables, use `print_r($data);`
 */
$output = array();
foreach (
$data->node_data_field_my_noderef_field_my_noderef_nid as $my_nid) {
    if (
$my_nid['nid']) {
      
$my_node = node_load($my_nid['nid']);
      
$output[] = $my_node->title;
    }
}
print_r(implode($output,","));
?>
pandaPowder’s picture

Is there any way to do this in drupal 7?

Thanks