I'd like to have support for fields which take multiple values. Currently, I looked at the code:

 function text_token_values($type, $object = NULL) {
    if ($type == 'field') {
      $item = $object[0];
      $tokens['raw']  = $item['value'];
      $tokens['formatted'] = $item['view'];
      return $tokens;
    }
  }

IMHO, this should at least be made clear in the documentation that this only retrieves the first value. Ideally though, I was thinking it should generate two tokens, one being a dynamic token (can this be done?) where if the field was Food the token would be: [node:field_food-raw-x], and someone could use Hi, I like node:field_food-raw-0 and node:field_food-raw-1. It would also be nice to have something like: node:field_food-raw-all which would return the values with a delim of some sort, of rendered through cck's theme_field.

What do you think? I can take this on, but I'd like some direction on what would be preferred.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

JacobSingh’s picture

Okay, I made a little patch for this (as a proto, only works with the text fields for now):

JacobSingh’s picture

Status: Active » Needs review

Can someone look at this?

greggles’s picture

The way that you've described it, my opinion is that this should be implemented in token2.x (see some discussion at token group page).

If you'd like to work towards that effort I personally would be very excited about it.

suit4’s picture

+1 for integration!

Great job! works absolutely flawless!

There is a typo in the path, the second line starting with
$tokens['text']['formatted-all']

should read
$tokens['text']['formatted-n']

doublejosh’s picture

You can also solve it like this...

Choose "Evaluate PHP in pattern."

<?php
$colorList= '';
$numColors= count($node->field_colorlist)-1;
for($i=0;$i<($numColors);$i++) {
   $colorNode= node_load($node->field_colorlist[$i]['nid']);
   $colorList.= $colorNode->title.' - ';
}
//last one
$colorNode= node_load($node->field_colorlist[$numColors]['nid']);
$colorList.= $colorNode->title;
return $colorList;
?>

My context is a list of colors to create a palette, where the title becomes a list of the colors chosen.

rdmillner’s picture

Does the OP's code work in 6?

ptrepoff’s picture

Version: 5.x-1.7 » 6.x-1.12

Is this a feature that is going to be implemented into tokens. If not what adjustments need to made to use this patch to work with the v6 version of tokens?

greggles’s picture

Project: Token » Content Construction Kit (CCK)
Version: 6.x-1.12 » 6.x-3.x-dev
Component: Code » content.module

All Token related code for 6.x is basically the same. CCK code has changed a bit and, one important distinction, all token code for 6.x should be in CCK directly.

markus_petrux’s picture

Version: 6.x-3.x-dev » 6.x-2.x-dev
Component: content.module » General
Status: Needs review » Closed (duplicate)

This is related to #272949: Token for mail address of user in CCK user reference field

(probably, the title on that issue will change when token is ready for CCK to implement these enhancements).

crusher82’s picture

Have you some news about that problem ?

ayalon’s picture

Title: Support for Multiple Entry Fields in CCK » Support for Multiple Entry Fields in CCK Token
Category: feature » task
Status: Closed (duplicate) » Needs review
FileSize
1.77 KB

I wanted to add a checkboxfield with multiple possibilities to an Email with token. This is not possible unless you use the patch.

I updated the patch for the latest cck version.

It's working perfect and it would be very good, if this could be committed.

Agileware’s picture

FileSize
12.18 KB
12.18 KB

I have another very similar solution to this problem.
When I started I forgot that I'd previously noticed this issue.

Anyway, here is my patch with a version also made for 6.x-3.x-dev.

I plan to make some additional modifications for 6.x-3.x-dev so that the
multiple values can be properly grouped by their multigroup, which is my original use case.

It provides support for text, number, node reference & user reference fields and
also uses a theme function for the token so that the separation of each value
can be customised. For example a themer can override it to output as an unordered list.

Note that it needs some testing for the reference fields as I am primarily using it for text.

I have currently made the default separator a
tag due to my requirements but
that can be changed to whatever people think is best.

Both patches are against latest dev versions.

Agileware’s picture

Category: task » feature
Agileware’s picture

FileSize
21.57 KB
18.55 KB

I have changed my approach a bit since tackling the task of fieldgroup tokens.

Here are new patches that include the following:
* Tokens for all values (for example raw-all & formatted-all)
* Tokens for individual deltas of fields for example (for example raw-# & formatted-# where you replace # with the delta you want)
* Tokens for fieldgroups, and multigroups in the case of cck 3. (for example raw, formatted, raw-titles & formatted-titles)
These tokens will print all fields of a specified group
The raw-titles & formatted-titles tokens also print the field names before each field of the group.
The fields will appear in the order they appear in the group for fieldgroups and multigroups.

Any tokens with multiple fields in them will run through the theme function:

theme_content_multiple_value_token($items = array(), $type = 'field')

where type is either field or group so the different token types can be differently themed.
This allows users to change the separator of the fields.

Both patches against latest devs.

Agileware’s picture

Title: Support for Multiple Entry Fields in CCK Token » Support for multiple entry fields and field groups in CCK token
Component: General » Token Integration
jday’s picture

Applied the patch, works GREAT, Thank YOU!

Update:

spoke too soon, the patch works great for multiple text value tokens, but I also have a filefield that allows multiple values and I'm not getting tokens for each file...

Agileware’s picture

Status: Needs review » Needs work

mmm, originally I was thinking that other modules like filefield & date do their own
tokens but in the case of fieldgroups & multigroups it needs to be handled by cck.

So the best way to do it without duplicating code would be to make cck call
the normal token_values functions for each field of the group from within
the groups token_values function.
Then it should work properly for all cck fields.

I was going to do it that way at one point anyway.

I will do another version of this patch to address this.

PS: However, these parts:

* Tokens for all values (for example raw-all & formatted-all)
* Tokens for individual deltas of fields for example (for example raw-# & formatted-# where you replace # with the delta you want)

will still have to be done by the other modules, so filefield has to implement that.
Ideally in the same manner as whatever is decided on here so it all works together well.

3dloco’s picture

+1

jsm174’s picture

Just applied the patch from #14 for 2.x. Awesome! Works great.

This patch should get committed.

Agileware’s picture

Yeah, but I still need to do a little bit more work on it Re: #16 & 17

I just haven't had time lately.

simonmetacci’s picture

subscribing, really need this to get fixed.
Im using the date module and want to print 'all' values out and as I can see they have to implement this aswell..

Looking forward for a patch for this issue<3

interestingaftermath’s picture

I cannot get the patch in #14 to work correctly. I know I am doing something wrong. Please help.

I have run the patch and I see the tokens in the list to choose from. I am using Notifications/Messaging to send out node creation emails. All of the tokens work except for the new tokens provided by this patch. They do not print anything in the e-mail.

It's a plain text e-mail and I have tried raw and formatted. I've tried many options in the Tokens/Displays options as well. Here is the latest of my email:

Items:
[group_order-raw-titles]
[group_order-raw]
[field_part_number-formatted-all] | [field_quantity_ordered-raw-all] | [field_description-raw-all]

I have a couple extra fields printing (as you can see) because I would like to see what they do. From that above this is what the e-mail looks like when it hits my inbox:

Items:
| |

What am I missing?

interestingaftermath’s picture

Status: Needs work » Active
Agileware’s picture

Status: Active » Needs work

Please don't change the status unless there is reason, this patch still needs work (See #16 & #17)

See here for information regarding what each of the statuses mean - http://drupal.org/node/156119

interestingaftermath’s picture

Sorry about that and thank you for the link. I wasn't sure of the meaning of the status options.

That said, I think the issues addressed in 16/17 do not apply to my issue. I believe I am just missing something because it seems you claimed that the patch does what I need it to. Any help?

interestingaftermath’s picture

I have figured this out. Not really sure how. Just upgraded to the latest dev version and it started working. Any idea how to do something like this?

<p>[field_jb_part_number-raw-0] | [field_quantity_ordered-raw-0] | [field_shipped-raw-0] | [field_description-raw-0]</p>
<p>[field_jb_part_number-raw-1] | [field_quantity_ordered-raw-1] | [field_shipped-raw-1] | [field_description-raw-1]</p>
<p>[field_jb_part_number-raw-2] | [field_quantity_ordered-raw-2] | [field_shipped-raw-2] | [field_description-raw-2]</p>
<p>[field_jb_part_number-raw-3] | [field_quantity_ordered-raw-3] | [field_shipped-raw-3] | [field_description-raw-3]</p>

But instead of those static delta numbers do some sort of a counter? If I do this...

<p>[field_jb_part_number-raw-all] | [field_quantity_ordered-raw-all] | [field_shipped-raw-all] | [field_description-raw-all]</p>

...it does exactly what it says. It prints ALL jb_part_number in a row, then ALL quantity_ordered in a row, etc.

I'm wanting to theme the contents of this multigroup in a table of some sort in an html e-mail. The results need to come out so that I can wrap them in tr/td tags, etc.

raw & raw-titles almost do the trick but for me they just print the fields down a straight line. How am I able to theme that somehow?

Agileware’s picture

if you are somewhere you can use php you could do something like this possibly (provided you have access to the $node)?

  $output = '<table>';
  foreach ($node->field_jb_part_number as $delta => $item) {
    $output .= '<tr>';
    $output .= "<td>[field_jb_part_number-raw-$delta]</td>";
    $output .= "<td>[field_quantity_ordered-raw-$delta]</td>";
    $output .= "<td>[field_shipped-raw-$delta]</td>";
    $output .= "<td>[field_description-raw-$delta]</td>";
    $output .= '</tr>';
  }
  $output .= '</table>';
  return $output;

$output would be a table like you require, but it depends on your situation as to whether you can use php like that.

Another way you could probably get what you want would be to use cck 6.x-3.x-dev and put your fields into a multigroup.
Then use the 6.x-3.x-dev version of this patch.
That would depend on if cck 6.x-3.x-dev is good for your situation.

interestingaftermath’s picture

Thank you for your response. This information is being sent out as an e-mail so I don't know if I am able to use PHP.

That said, I am using cck 6.x-3.x-dev and the 6.x-3.x-dev version of the above patch AND I have these fields in a multigroup. What am I doing wrong?

interestingaftermath’s picture

As I mentioned above, I am using the CCK and PATCH versions that you recommend so many there is an easier way to go about doing this.

I tried the PHP code that you so kindly wrote for me. I made sure that all filters were set to PHP code (I am using the Messaging & Notifications module to send out a e-mail containing the contents of this multigroup on node creation). However, the PHP is not printing anything in the e-mail and I am getting an error on node creation.

warning: Invalid argument supplied for foreach() in C:\Inetpub\vhosts\test.com\httpdocs\includes\common.inc(1695) : eval()'d code on line 134.

This may be pointless information, but as I change the line that the PHP snippet appears in the e-mail body, the error code "line" number changes accordingly.

Any ideas would be very appreciated!

interestingaftermath’s picture

I'd still like to know what the other method you mention was when you weren't sure if I was using the 3.x-dev version of CCK and the PATCH. I did figure out the issue when using Rules to send the e-mail. I added "node:" to your code. Thanks!

<?php
  $output = '<table>';
  foreach ($node->field_jb_part_number as $delta => $item) {
    $output .= '<tr>';
    $output .= "<td>[node:field_jb_part_number-raw-$delta]</td>";
    $output .= "<td>[node:field_quantity_ordered-raw-$delta]</td>";
    $output .= "<td>[node:field_shipped-raw-$delta]</td>";
    $output .= "<td>[node:field_description-raw-$delta]</td>";
    $output .= '</tr>';
  }
  $output .= '</table>';
  return $output;
?>

<?php print $output;?>
interestingaftermath’s picture

Hello again. The code posted above works great in Rules -> Mail to Arbitrary address -> event "after saving new content". However, it does NOT work for event "after updating existing content".

Here is what I found by doing print_r($node) in both e-mails.

"After saving new content" prints

[field_jb_part_number] => Array ( [0] => Array ( [value] => Test [_remove] => 0 [_weight] => 0 ) )

"After updating existing content" prints

[field_jb_part_number] => Array ( [0] => Array ( [value] => Test1 ) )

Any ideas?

jday’s picture

applied the patch in #14 to cck 6.x-2.7,

but multiple value tokens are blank ( [node:field_myfield-formatted-all] )

twistedindustries’s picture

Same here...

letzel’s picture

subscribe

Itangalo’s picture

I'm feeling a bit stupid finding a lot of issues about Token and multiple value CCK fields and multiple taxonomy terms. I took as an exercise to code this myself, and ended up with something that looks pretty decent.

Code and demo screencast can be found here, if anyone's interested: http://nodeone.se/blogg/learning-to-code-array-tokens-module

roderik’s picture

Italango has now turned this into a project on d.o, check out http://drupal.org/project/token_array

w.r.t. the original subject: token_array generates tokens for 'multiple entry fields', not for 'field groups'.

NancyDru’s picture

sub

mjstone323’s picture

subscribe

mjstone323’s picture

I wish to use tokens for multigroups in some triggered actions. I'm hoping very fervently that someone is working on this, since an important aspect of the project I'm currently working on is not functioning due to the fact that there are no tokens for multigroups, and I don't know how to create them, although I'm trying to learn.

I took a look at the way the information is stored in the database. Unlike single-field content, which is all stored in one row of a table, with a key that indicates the id of the main node, each field in multigroup field content is normalized an additional level, and is stored in its own table, with two keys (the main node id and a "delta" indicating the placement of each item of data in an array).

I would like to try my hand at creating tokens myself, but my understanding of PHP is so rudimentary that I can't yet even write a token for a regular field, let alone one with two keys.

Also - could Rules handle any kind of token, or would a token for a multigroup would be incompatible without a change to Rules? Finally, would the Token module need to be updated with some protocol for handling this type of token?

I will continue to try to figure this out, but if anyone is working on this, please share what you have learned so far.

Edit: Just found this: http://drupal.org/node/690140#comment-3541778
and I'm realizing I just don't have the skills to figure this out on my own. Really need tokens for multigroup fields included in the CCK module.

Itangalo’s picture

@mjstone323: I think it's a good idea to start a new issue for this – your feature request risks being forgotten here (since this issue deals with field groups and multiple entry fields).

Rules *will* work with any kind of tokens - no worries there.

Good luck!

mjstone323’s picture

Thanks! I appreciate your advice - I'll open a new feature request.

Murz’s picture

subscribe

creando sensaciones’s picture

subscribe